《Android编程权威指南(第3版)》:第十章挑战练习

10.6:实现高效的RecyclerView刷新

在CrimeListFragment新建一个变量保存点击时的位置 

public class CrimeListFragment extends Fragment {

    int UpdatePosition = 0;

    private void updateUI() {
        CrimeLab crimeLab = CrimeLab.get(getActivity());
        List crimes = crimeLab.getCrimes();

        if (mAdapter == null) {
            mAdapter = new CrimeAdapter(crimes);
            mCrimeRecyclerView.setAdapter(mAdapter);//将RecyclerView与Adapter绑定
        }else{
            mAdapter.notifyItemChanged(UpdatePosition);
        }
    }


    private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @Override
        public void onClick(View view){
            Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
            UpdatePosition = getAdapterPosition();
            startActivity(intent);
        }
    }

}

10.7:优化CrimeLab的表现

在CrimeLab中加一个TreeMap(搞了半天才发现Map是接口,不能直接用)

每个UUID对应的Crime在List的编号

public class CrimeLab {

    private TreeMap mtable;


    private CrimeLab(Context context){
        mtable = new TreeMap();
        for (int i = 0; i < 100; i++){
            mtable.put(crime.getId(), (Integer)mCrimes.size());
        }
    }

    public Crime getCrime(UUID id){
        return mCrimes.get(mtable.get(id));
    }

}

 

你可能感兴趣的:(《Android编程权威指南(第3版)》:第十章挑战练习)