1. 添加依赖:compile
'com.android.support:recyclerview-v7:23.1.1',
也可以直接添加高版本的design包,会自动添加recyclerView包。
2. 在xml布局文件中引用RecyclerView
3. 继承RecyclerView.Adapter<RecyclerView.ViewHolder>构造RecyclerView的数据适配器
4. 在Activity中给RecyclerView设置布局管理器(三种布局管理器分别是
LinearLayoutManager
,
GridLayoutManager
以及
StaggeredGridLayoutManager
)
5. 封装item的点击事件以及长按事件
6. 建议将点击事件写在自定义ViewHolder中,要想获得点击item的位置一定要使用getLayoutPosition方法来获得。
注:RecyclerView有一个致命的缺点,每个item之间Google没有实现Divider的设定,解决方案有:
1 设置maigin属性。
2 实现RecyclerView.ItemDecoration:一般可从github上找已做好的框架
3 使用CardView进行item布局,且有卡片的视觉效果
@Override
protected void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
rv
= (RecyclerView) findViewById(R.id.
recyclerview
);
final
MyRecylerViewAdapter adp =
new
MyRecylerViewAdapter(
this
,
beans
);
adp.setPcb(
new
MyRecylerViewAdapter.PositionCallBack() {
@Override
public void
postPosition(
int
pos) {
beans
.remove(pos);
adp
.addBeans(
beans
,pos);
}
});
rv
.setAdapter(adp);
// rv.setLayoutManager(getLinearLayoutManager());//horizonal
// rv.setLayoutManager(getVirticalLayoutManager());//virtical
rv
.setLayoutManager(getStaggerLayoutManager());//必须指定
}
/***四种样式
public
RecyclerView.LayoutManager getLinearLayoutManager() {
return new
LinearLayoutManager(
this
, LinearLayoutManager.
HORIZONTAL
,
false
);
}
public
RecyclerView.LayoutManager getVirticalLayoutManager() {
return new
LinearLayoutManager(
this
, LinearLayoutManager.
VERTICAL
,
false
);
}
//流布局
public
RecyclerView.LayoutManager getStaggerLayoutManager() {
return new
StaggeredGridLayoutManager(
3
, StaggeredGridLayoutManager.
VERTICAL
);
}
//表格
public
RecyclerView.LayoutManager getGridLayoutManager() {
return new
GridLayoutManager(
this
,
3
);
}
**/
***********************************************************************
//监听器 实现效果点击图片删除
public class
MyRecylerViewAdapter
extends
RecyclerView.Adapter<MyRecylerViewAdapter.MyHolderView> {
private
Context
mContext
;
private
List<CardBean>
beans
;
private
LayoutInflater
mInflater
;
public
MyRecylerViewAdapter(Context context,List<CardBean> beans){
mContext
=context;
this
.
beans
=beans;
mInflater
=LayoutInflater.
from
(context);
}
@Override
public
MyHolderView onCreateViewHolder(ViewGroup parent,
int
viewType) {
View convertView=
mInflater
.inflate(R.layout.
item_recylerview_cardview
,
null
);
return new
MyHolderView(convertView);
}
@Override
public void
onBindViewHolder(MyHolderView holder,
int
position) {
CardBean bean=
beans
.get(position);
holder.tv.setText(bean.getImgTitle());
holder.img.setImageResource(bean.getImgId());
}
@Override
public int
getItemCount() {
return
beans
.size();
}
class
MyHolderView
extends
RecyclerView.ViewHolder{
public
TextView
tv
;
public
ImageView
img
;
public
MyHolderView(View convertView){
super
(convertView);
tv
= (TextView) convertView.findViewById(R.id.
tv
);
img
= (ImageView) convertView.findViewById(R.id.
img
);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getLayoutPosition();
if(pcb!=null){
pcb.postPosition(position);
}
}
});
}
}
public interface
PositionCallBack{
public void
postPosition(
int
pos);
}
PositionCallBack
pcb
;
public void
setPcb(PositionCallBack pcb) {
this
.
pcb
= pcb;
}
public void
addBeans(List<CardBean> beans,
int
pos){
this
.
beans
=beans;
notifyItemRemoved(pos);
}
}