把原本使用的Adapter的notifyDataSetChanged方法换成定位刷新RecyclerView.Adapter的notifyItem。


1.在CrimeListFragment里面定义一个全局变量

 1 private static int mCrimeIndex; 

2.修改CrimeListFragment下的onClick方法

1  @Override
2         public void onClick(View v) {
3             Intent intent = CrimeActivity.newIntent(getActivity(),mCrime.getmId());
4             mCrimeIndex = getAdapterPosition();//返回数据在Adapter中的位置
5             //Log.d("onClick","mCrimeIndex "+mCrimeIndex);
6             startActivity(intent);
7         }

3.修改updateUI方法

 1 private void updateUI(){
 2         CrimeLab crimeLab = CrimeLab.get(getActivity());
 3         List<Crime> crimes = crimeLab.getmCrimes();
 4         if(mAdapter == null){
 5             mAdapter = new CrimeAdapter(crimes);
 6             mCrimeRecyclerView.setAdapter(mAdapter);
 7         }else {
 8             //重绘当前可见区域
 9             //mAdapter.notifyDataSetChanged();
10 
11             //部分重绘
12             mAdapter.notifyItemChanged(mCrimeIndex);
13         }
14     }

4.运行效果:

挑战练习10.6 实现高效的RecyclerView刷新

 

5.用调试信息输出看看mCrimeIndex的变化,可以得知后退时没有变化

挑战练习10.6 实现高效的RecyclerView刷新

 


 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2022-02-17
  • 2022-01-22
  • 2021-07-21
  • 2021-10-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2022-12-23
  • 2021-07-31
相关资源
相似解决方案