如何停止刷新recyclerview数据每次滚动到顶部位置android [英] How to stop refreshing recyclerview data scroll to top position android everytime

查看:74
本文介绍了如何停止刷新recyclerview数据每次滚动到顶部位置android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 recyclerview 制作类似于视频的布局.我做了一个 recyclerview,它在一定时间间隔后更新列表,但问题是数据更新后它会自动滚动到顶部位置.我想做一些像视频一样的东西.https://youtu.be/omcS-6LeKoo我试过使用来自 SO 的链接RecyclerView 在更改适配器数据时滚动到顶部位置 RecyclerView notifyDataSetChanged 滚动到顶部位置但无法解决.下面是我的尝试

I am trying to make a layout with recyclerview something like the video. I made a recyclerview which update list after certain interval but the problem is after data update it scroll to top position automatically. I want to make something like the video. https://youtu.be/omcS-6LeKoo I have tried with link from SO RecyclerView scrolls to top position when change the adapter data RecyclerView notifyDataSetChanged scrolls to top position but unable to solve. below is my attempt

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

        recyclerView = findViewById(R.id.recyclerViewId);

        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                Toast.makeText(getApplicationContext(),"Updating",Toast.LENGTH_LONG).show();
                listShow();
                handler.postDelayed(this,1000);

            }
        },1000);

    }

void listShow(){

        retrofitApiCall = RetrofitInstance.getRetrofitInstance().create(RetrofitApiCall.class);
        Call<ModelClass_JSONParse> getDetails = retrofitApiCall;
        anime = ExtendedAnime.getAll();

        getDetails.enqueue(new Callback<ModelClass_JSONParse>() {
            @Override
            public void onResponse(Call<ModelClass_JSONParse> call, 
         Response<ModelClass_JSONParse> response) {
                Log.v("Res",response.toString());
                getWithValues = new HashMap<>();

                if (response.isSuccessful()){

                        list.add(mModelClass_adapter);
                    }
                    adapter = new Adapter(getApplicationContext(),list);
                    StaggeredGridLayoutManager layoutManager = new 
                    StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onFailure(Call<ModelClass_JSONParse> call, Throwable t) {
                Log.v("Res",call.toString());
            }
        });
    }

推荐答案

问题可能是因为您在网络回调方法 onResponse() 中设置了新的适配器引用.尝试在 onCreate 中设置适配器,然后在回调中更新数据集.

The problem is probably because of you are setting new adapter reference in network callback method onResponse(). Try setting adapter in onCreate and then update dataset in callback.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recyclerViewId);
    recyclerView.setAdapter(yourAdapter);
}

在网络回调中,

@Override
        public void onResponse(Call<ModelClass_JSONParse> call, 
     Response<ModelClass_JSONParse> response) {
            Log.v("Res",response.toString());
            getWithValues = new HashMap<>();

            if (response.isSuccessful()){

                adapter.setDataSet(newDataList) //not change adapter reference,only update data set  

            }
        }

在您的适配器中实现 setDataSet() 方法以更新如下列表.

Implement setDataSet() method in your adapter to update list like below.

class YourAdapter extends RecyclerView.Adapter<>{
      priavate List<> list = new ArrayList();

      public void setDataSet(newList:List<>){ 
          list.clear();
          list.addAll(newList);
          notifyDataSetChanged();
      }
}

这篇关于如何停止刷新recyclerview数据每次滚动到顶部位置android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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