当我更新时,Recyclerview位置更改为第一个位置 [英] Recyclerview Position changing to first position when I Update

查看:543
本文介绍了当我更新时,Recyclerview位置更改为第一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我滚动RecyclerView时,我正在使用onLoadMoreListener.当您滚动到底部时,它将更新RecyclerView数据.更新Recyclerview数据后. Recyclerview返回顶部.理想情况下,我想保留在RecyclerView中的位置.关于我应该如何做的任何想法?

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

推荐答案

尝试一下,

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

这篇关于当我更新时,Recyclerview位置更改为第一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆