简体   繁体   中英

The position of addOnScrollListener is resets to top when new data is loaded after scrolling

I have a recycler view and addOnScrollListener is implemented on it. The following is my api http://uat.nirc.com.np:8080/GWP/inventory/getInventoryRequestOnMobile?memberId=2&offset=0

In my api there is offset with each value containing max 10 items. Following is my main class where addOnScrollListener is implemented.

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();
            }

        }
    });

The following is getData function.

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

And the following is datacall function.

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());
        }
    });
}

In each token there are 10 items. After Scrolling to bottom new data is loaded and token is increased by +9. But while fetching the new data, the position of scrollListener resets to top, ie along with new data being fetched at bottom, the recyclerView is reseted to top.

Can somebody please help me? I want the position of scrollListener to stay in that exact last position.

while you are getting data on the second time use adapter.notifydatasetchanged(); instead of set adapter in your case you are using adapter.notifydatasetchanged(); and on the second line you are again recyclerView.setAdapter(adapter); setting adapter on recyclerview their is no need to set adapter on the recycler view on the second time when you are loading more items in the list just do not set adapter on the list in second and this should do the trick

Do like this

@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);
}

and in your dataCall method

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());
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM