Search code examples
androidandroid-recyclerviewinotifypropertychangednotifydatasetchanged

After Scrolling, Recycler moves to first postion


I face difficulty when working using recyclerview and Gridview. The problem is when my application success load next page (next data), the recyclerview always back to the top.

I want the recyclerview start from last index.

Example: First-page load 10 items, after success loan next page, the recycler view start scrolling from item 8.

For resolve that problem, i have tried all the solution on StackOverflow still get nothing.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                isScrolling = true;
            }

        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            currentItems = manager.getChildCount();
            totalItems = manager.getItemCount();
            scrolloutItems = manager.findFirstVisibleItemPosition();

            if (isScrolling && (currentItems + scrolloutItems == totalItems)) {
                isScrolling = false;
                getdata(pagetoken);

                //getdata(pagetoken);

                Toast.makeText(MainActivity.this, "Scrolling", Toast.LENGTH_SHORT).show();

            }
        }
    });

}


public void getdata() {
    progress.setVisibility(View.VISIBLE);
    ApiClient.getApis().getblogdata(Constants.key).enqueue(new Callback<MyModel>() {
        @Override
        public void onResponse(Call<MyModel> call, Response<MyModel> response) {
            if (response.isSuccessful()) {
                assert response.body() != null;
                arraylist.addAll(response.body().items);
            }

            if (arraylist != null) {
                adapter = new Data_Adapter(MainActivity.this, arraylist);
                recyclerView.setAdapter(adapter);
            }

            pagetoken = response.body().nextPageToken;

            swiprefresh.setRefreshing(false);
            progress.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<MyModel> call, Throwable t) {
            Toast.makeText(MainActivity.this, "\t \t Connection Error : \n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    });
}

public void getdata(String pageToken) {
    ApiClient.getApis().getblogdata(Constants.key, pageToken).enqueue(new Callback<MyModel>() {
        @Override
        public void onResponse(Call<MyModel> call, Response<MyModel> response) {

            if (response.isSuccessful()) {
                assert response.body() != null;
                arraylist.addAll(response.body().items);
            }

            if (arraylist != null) {
                adapter = new Data_Adapter(MainActivity.this, arraylist);
                recyclerView.setAdapter(adapter);
            }
            if (response.body().nextPageToken != null) {
                pagetoken = response.body().nextPageToken;
            }
            Log.d("retro", "" + arraylist.size());

            swiprefresh.setRefreshing(false);
        }

        @Override
        public void onFailure(Call<MyModel> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Connection Error : \n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    });
}

Solution

    • Issue :- RecyclerView goes to top position after added second page data

      • Reason :- You are making new instance of adapter all time

        In your case adapter is Data_Adapter and you are creating with

        adapter = new Data_Adapter(MainActivity.this, arraylist);
        
      • Solution :- make only one instance of adapter

    • Example:- RecyclerView goes to top after added second page data

    • create arraylist :-

      private ArrayList<String> catList = new ArrayList<>();
      
    • on onCreate [ to initialize your adapter and asign it to recycle view]

      if (arraylist != null) {
          adapter = new Data_Adapter(MainActivity.this, arraylist);
          recyclerView.setAdapter(adapter);
      }
      
    • in getdata method [ in onResponse to notify adapter ] :-

      if (arraylist != null && arraylist.size() > 0) {
          // to notify adapter
          adapter.notifyItemInserted(adapter.getItemCount());
      }