1.导入
compile 'com.android.support:recyclerview-v7:25.0.2'
2.准备工作(间隔)
3.开始使用 - xml 的编写 - 简单的Adapter编写
/**
* author Phyooos
* date 16/5/3.
*/
public class CrAdapter extends RecyclerView.Adapter<CrViewHodle>{
ArrayList<String> mdata ;
private Context mContext;
private LayoutInflater inflater;
//构造传参
public CrAdapter(Context mContext ,ArrayList<String> mdata) {
this.mdata = mdata;
this.mContext = mContext; this.inflater = LayoutInflater.from(mContext);
}
//绑定数据
@Override
public void onBindViewHolder(CrViewHodle holder, int position) {
holder.textView.setText(mdata.get(position));
}
//加载iten布局并创建ViewHolder
@Override
public CrViewHodle onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_recycle,parent,false);
CrViewHodle hodle = new CrViewHodle(view); return hodle;
}
//条目总数
@Override
public int getItemCount() {
return mdata.size();
}
}
//各个控件声明,初始化
class CrViewHodle extends RecyclerView.ViewHolder {
TextView textView;
public CrViewHodle(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
}
}
4.在Activity中使用
//获得
CrAdapter crAdapter = new CrAdapter(this,mDatas);
//设置
CrAdapter mRecyclerView.setAdapter(crAdapter);
//Context VERTICAL或者HORIZONTAL的样式
manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(manager);
//设置Item动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//设置间隔 Context 以及mRecyclerView样式,与上面一致
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));```
###5.添加点击事件这个大概有2种比较方便的方法来实现但是个人比较倾向于在onBindViewHolder中给控件带上参数来使用setOnClickListener
例子:
1)定义一个接口
/**
- @author: phyooos on 16.6.22 09:58.
- @E-mail: phyooos@163.com
*/
public interface OnRecycleItemListener {
void OnRecycleItemClick(View v,Object o);
}
我发现Object o每一次都需要转化,很麻烦也可以这样使用,
public interface OnRecycleItemListener <T>{
void OnRecycleItemClick(View v,T o);
}
PS;参数什么的自己定义即可,用什么就定义什么
2)在onBindViewHolder给要添加的view设置点击事件
@Override
public void onBindViewHolder(TypeViewHolder holder, int position) {
ActivityItemModel itemModel = model.getDatalist().get(position);
holder.textView.setText(itemModel.getName());
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnRecycleItemClick(v,itemModel);
}
});
}
使用setOnClickListener并在其中调用接口中的OnRecycleItemClick
3)那么上面listener对象需要定义出来
private OnRecycleItemListener listener;
4)最后一步就是定义一个对外开放的方法
public void addRecycleItemListener(OnRecycleItemListener listener){
this.listener = listener;
}```
到此为止,你可以在activity中给创建出来的adapter实例添加点击事件,并在回调中处理各种事件
5)在activity中使用
//在activity中对recycleView的adapter(listAdapter)添加子项目的点击事件
listAdapter.addRecycleItemListener(listener);
-------------------------------------------------------------
OnRecycleItemListener<Prize> listener = new OnRecycleItemListener<Prize>() {
@Override
public void OnRecycleItemClick(View v, final Prize p) {
switch (v.getId()){
case R.id.act_list_edit:
//在这里处理Item的点击事件即可
break;
}
}
};```