更新RecyclerView的好方法,更新recyclerview
一般在使用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); } } }