更新RecyclerView的好方法

时间:2022-01-16 22:56:19

  一般在使用RecyclerView的时候不免要修改RecyclerView的数据,使用notifyDataSetChanged()来刷新界面,但是当数据比较多,而只是修改了一点的数据,或者刷新比较频繁,这样就会导致界面的卡顿问题,用户交互特别不好。

  这个时候就需要只是修改需要修改的数据,不要将数据全部进行更新,这样就可以解决问题。

  局部更新的代码如下:

  

private int position;//当前recyclerview的position

@BindView(R.id.speak_valuate_recycler_view)
RecyclerView recyclerView; private LinearLayoutManager mRecyclerViewLayoutManager; mRecyclerViewLayoutManager = new LinearLayoutManager(this);
mRecyclerViewLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(mRecyclerViewLayoutManager); private void changVolume(final int volume) {
int first = mRecyclerViewLayoutManager.findFirstVisibleItemPosition();
int last = mRecyclerViewLayoutManager.findLastVisibleItemPosition(); if (position >= first && position <= last) {
View view = recyclerView.getChildAt(position - first);
if (recyclerView.getChildViewHolder(view) instanceof SpeakContentAdapter.SpeakContentHolder) {
//修改数据
ProgressImageView progressImageView = (ProgressImageView) view.findViewById(R.id.speak_item_record);
progressImageView.setProgress(volume);
}
}
}