RecyclerView使用
布局中加入RecyclerView后,对应的程序中需要走三步:
一、设置布局管理器
三种布局管理器:线性布局LinearLayoutManager,网格布局GridLayoutManager,瀑布流布局StaggeredGridLayoutManager
LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
GridLayoutManager manager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayouManager.VERTICAL);
二、然后将RecyclerView和布局管理器联动起来
mRecyclerView.setLayoutManager(layout );
三、绑定适配器
adapter=new Adapter(this ,mData);
mRecyclerView.setAdapter(adapter);
如果要加入动画效果,则
mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this , LinearLayoutManager.VERTICAL));
mAdapter.setOnItemClickListener(new BasicAdapter.OnItemClickListener() {
@Override
public void onItemClick (int pos, View itemView) {
Toast.makeText(BasicActivity.this , "pos::" + pos, Toast.LENGTH_SHORT).show();
}
});
注:RecyclerView中动画的添加及使用,在下一篇文章中将给出使用实例与操作说明。