滚动后加载新数据时,addOnScrollListener的位置将重置为顶部

时间:2018-09-05 05:17:03

标签: android api android-recyclerview retrofit infinite-scroll

我有一个回收站视图,并在其上实现了addOnScrollListener。以下是我的api http://uat.nirc.com.np:8080/GWP/inventory/getInventoryRequestOnMobile?memberId=2&offset=0

在我的api中,每个值最多包含10个项目有一个偏移量。以下是我的主要类,其中实现了addOnScrollListener。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
    {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) 
       {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
            {
                isScrolling = true;
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            currentItems = layoutManager.getChildCount();
            totalItems = layoutManager.getItemCount();
            scrollOutItems = layoutManager.findFirstVisibleItemPosition();

            if(isScrolling && (currentItems + scrollOutItems == totalItems ))
            {
                isScrolling = false;
                getData();
            }

        }
    });

以下是getData函数。

 public void getData()
   {
    if(token <= reqCount){
        progressBar.setVisibility(View.VISIBLE);
        token = token + 9;
        datacall(token);
    }
}

以下是数据调用函数。

public void datacall(int token){
    apiInterface = ApiClient.getApiClient().create(TalikaApiInterface.class);
    Call<JinsiAnirodhTalikaModel> call = apiInterface.getData(id,token);
    call.enqueue(new Callback<JinsiAnirodhTalikaModel>() {
        @Override
        public void onResponse(Call<JinsiAnirodhTalikaModel> call, Response<JinsiAnirodhTalikaModel> response) {
            requestCount = Integer.parseInt(response.body().requestCount);
            for(Data a :response.body().data){
                posts.add(a);
            }
            adapter = new JinsiAnurodhTalikaAdapter(posts,getApplicationContext());
             progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JinsiAnirodhTalikaModel> call, Throwable t) {
            Log.e("error",t.getMessage());
        }
    });
}

每个令牌中有10个项目。滚动到底部后,将加载新数据,并且令牌增加+9。但是,在获取新数据时,scrollListener的位置将重置为顶部,即在底部获取新数据的同时,recyclerView将重置为顶部。

有人可以帮我吗?我希望scrollListener的位置保持在那个最后的位置。

2 个答案:

答案 0 :(得分:2)

第二次获取数据时,请使用adapter.notifydatasetchanged();而不是设置适配器,而在第二行中,您将再次adapter.notifydatasetchanged();在recyclerview上设置适配器当您在列表中加载更多项目时,无需在第二次在回收者视图上设置适配器,只是不要在第二秒钟在列表上设置适配器,这应该可以解决问题

答案 1 :(得分:1)

这样做

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
//  Your other code
    ...

    adapter = new JinsiAnurodhTalikaAdapter(posts,getApplicationContext());
    recyclerView.setAdapter(adapter);
}

并在您的dataCall方法中

public void datacall(int token){
    apiInterface = ApiClient.getApiClient().create(TalikaApiInterface.class);
    Call<JinsiAnirodhTalikaModel> call = apiInterface.getData(id,token);
    call.enqueue(new Callback<JinsiAnirodhTalikaModel>() {
        @Override
        public void onResponse(Call<JinsiAnirodhTalikaModel> call, Response<JinsiAnirodhTalikaModel> response) {
            requestCount = Integer.parseInt(response.body().requestCount);
            for(Data a :response.body().data){
                posts.add(a);
            }

            progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<JinsiAnirodhTalikaModel> call, Throwable t) {
            Log.e("error",t.getMessage());
        }
    });
}