2

I am using onLoadMoreListener when i scroll RecyclerView. It will update the RecyclerView data when you scroll to bottom. After updating the Recyclerview data. The Recyclerview goes back up to the top. Ideally, I would like to retain the position in the RecyclerView. Any thoughts on how I should go about doing this?

mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
              new GroupData().execute();
            }
        });


InnerClass

    public static class GroupData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
           try{
             GroupList gm = new GroupList();
             .....
             groupvalues.add(gm);
            } catch (Exception e){
               Log.d("Grouplist ", e.toString());
           }
          return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
             super.onPostExecute(aVoid);
             mAdapter = new GroupAdapter(groupvalues,mrecyclerview);
             mrecyclerview.setAdapter(mAdapter);
             progressDialog.dismiss();
             mAdapter.notifyDataSetChanged();
             }
           }
         }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dev Tamil
  • 629
  • 1
  • 9
  • 25
  • 1
    it seem like you refresh your Recyclerview not load more. try to change your logic first – Linh Jan 21 '17 at 07:15

3 Answers3

2

When you are loading you must be knowing the position of top element of new loaded items. just call:

recyclerView.scrollToPosition(position);

or just save the last know position of your recyclerView and Scroll to that position when you load more items

Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
2

Try this,

public static class GroupData extends AsyncTask<Void, Void, Void> {
    int size = 0;

    @Override
    protected void onPreExecute() {
        size = groupvalues.size();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            GroupList gm = new GroupList();
            .....
            groupvalues.add(gm);
        } catch (Exception e) {
            Log.d("Grouplist ", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mAdapter.notifyItemRangeChanged(size, groupvalues.size() - size);
        progressDialog.dismiss();
    }
}
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
1

Just notify your adapter in onPostExecute. No need to initialise your adapter again.

    @Override
    protected void onPostExecute(Void aVoid) {
         super.onPostExecute(aVoid);
         progressDialog.dismiss();
         mAdapter.notifyDataSetChanged();
    }

Initialise your adapter in onCreate

mAdapter = new GroupAdapter(groupvalues,mrecyclerview);
mrecyclerview.setAdapter(mAdapter);
Suresh Kumar
  • 2,014
  • 3
  • 19
  • 32