添加依赖
compile 'com.android.support:recyclerview-v7:25.3.0'
然后就可以在布局文件里用RecyclerView控件了
- RecyclerView类是ViewGroup的子类,每一个列表项都是作为一个view子对象显示的
RecyclerView负责回收和定位屏幕上子项的View
定位是通过LayoutManager来实现的
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
- adapter是控制器对象,从模型层获取数据,然后提供给recyclerView显示
mRecyclerView.setAdapter(mAdapter);
adapter还负责创建必要的ViewHolder,绑定ViewHolder与模型层数据
适配器就在于把模型层数据(crimes)与视图层(ViewHolder)适配然后在适配进RecylerView中FindViewById(int)是耗时又耗内存的操作
ViewHolder起到了保存FindViewById方法的成果的作用,大大减少的消耗
CrimeAdapter
public class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes=crimes;
}
@Override
//获取单项布局
public CrimeHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
View view=layoutInflater.inflate(R.layout.list_item_crime,parent,false);
return new CrimeHolder(view);
}
@Override
//将单项布局的控件用数据来配对填充
public void onBindViewHolder(CrimeHolder holder,int position)
{
Crime crime=mCrimes.get(position);
holder.bindHolder(crime);
}
@Override
//获取总项数
public int getItemCount(){
return mCrimes.size();
}
}
}
- ViewHolder的作用是存下并加载列表项的布局,所以在其构造函数中要传入布局文件,也可以在它构造的时候绑定监听事件
CrimeHolder
public class CrimeHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
private Crime mCrime;
public TextView mTitleTextView;
public TextView mDateTextView;
public CheckBox mCheckBox;
public CrimeHolder(View itemView){
super(itemView);
//给列表项添加监听器也可以给列表项里的控件加
itemView.setOnClickListener(this);
mTitleTextView=(TextView)itemView.findViewById(R.id.title_text);
mDateTextView=(TextView)itemView.findViewById(R.id.date_text);
mCheckBox=(CheckBox)itemView.findViewById(R.id.check_box);
}
@Override
public void onClick(View v){
Toast.makeText(getActivity(),mCrime.getTitle()+"clicked!",Toast.LENGTH_SHORT).show();
}
//把绑定的代码写在viewHolder里然后在adapter里调用该方法更好
public void bindHolder(Crime crime)
{
mCrime=crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mCheckBox.setChecked(mCrime.getSolved());
}
}
因为CrimeListActivity和CrimeActivity都是管理一个Fragment的Activity,而且用的是同样的布局,所以可以写一个抽象类来封装提交fragment事务的操作
因为管理的是不同的fragment所以需要让Activity重写创建fragment的部分
public abstract class SingleFragmentActivity extends FragmentActivity{
//不能用private因为要在子类里去实现嘛
protected abstract Fragment createNewFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
FragmentManager fragmentManager=getSupportFragmentManager();
Fragment fragment=fragmentManager.findFragmentById(R.id.fragment_container);
if(fragment==null){
fragment=createNewFragment();
fragmentManager.beginTransaction().add(R.id.fragment_container,fragment).commit();
}
}
}
CrimeListFragment的完整代码
public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerView;
private CrimeAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.crimelist_recycleview,container,false);
mRecyclerView=(RecyclerView)view.findViewById(R.id.crimes_recycle_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI(){
CrimeLab crimeLab=CrimeLab.get(getActivity());
mAdapter=new CrimeAdapter(crimeLab.getCrimes());
mRecyclerView.setAdapter(mAdapter);
}
public class CrimeHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
private Crime mCrime;
public TextView mTitleTextView;
public TextView mDateTextView;
public CheckBox mCheckBox;
public CrimeHolder(View itemView){
super(itemView);
itemView.setOnClickListener(this);
mTitleTextView=(TextView)itemView.findViewById(R.id.title_text);
mDateTextView=(TextView)itemView.findViewById(R.id.date_text);
mCheckBox=(CheckBox)itemView.findViewById(R.id.check_box);
}
@Override
public void onClick(View v){
Toast.makeText(getActivity(),mCrime.getTitle()+"clicked!",Toast.LENGTH_SHORT).show();
}
//把绑定的代码写在viewHolder里然后在adapter里调用该方法更好
public void bindHolder(Crime crime)
{
mCrime=crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mCheckBox.setChecked(mCrime.getSolved());
}
}
public class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes=crimes;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
View view=layoutInflater.inflate(R.layout.list_item_crime,parent,false);
return new CrimeHolder(view);
}
@Override
public void onBindViewHolder(CrimeHolder holder,int position)
{
Crime crime=mCrimes.get(position);
holder.bindHolder(crime);
}
@Override
public int getItemCount(){
return mCrimes.size();
}
}
}