這期內容當中小編將會給大家?guī)碛嘘PAndroid應用中怎么對RecyclerView進行更新,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
一般在使用RecyclerView的時候不免要修改RecyclerView的數(shù)據(jù),使用notifyDataSetChanged()來刷新界面,但是當數(shù)據(jù)比較多,而只是修改了一點的數(shù)據(jù),或者刷新比較頻繁,這樣就會導致界面的卡頓問題,用戶交互特別不好。
這個時候就需要只是修改需要修改的數(shù)據(jù),不要將數(shù)據(jù)全部進行更新,這樣就可以解決問題。
局部更新的代碼如下:
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) { //修改數(shù)據(jù) ProgressImageView progressImageView = (ProgressImageView) view.findViewById(R.id.speak_item_record); progressImageView.setProgress(volume); } } }