如何每次都停止刷新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();
      }
}