1.導(dǎo)入
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;
//構(gòu)造傳參
public CrAdapter(Context mContext ,ArrayList<String> mdata) {
this.mdata = mdata;
this.mContext = mContext; this.inflater = LayoutInflater.from(mContext);
}
//綁定數(shù)據(jù)
@Override
public void onBindViewHolder(CrViewHodle holder, int position) {
holder.textView.setText(mdata.get(position));
}
//加載iten布局并創(chuàng)建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;
}
//條目總數(shù)
@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);
//設(shè)置
CrAdapter mRecyclerView.setAdapter(crAdapter);
//Context VERTICAL或者HORIZONTAL的樣式
manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(manager);
//設(shè)置Item動畫
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//設(shè)置間隔 Context 以及mRecyclerView樣式,與上面一致
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));```
###5.添加點擊事件這個大概有2種比較方便的方法來實現(xiàn)但是個人比較傾向于在onBindViewHolder中給控件帶上參數(shù)來使用setOnClickListener
例子:
1)定義一個接口
/**
- @author: phyooos on 16.6.22 09:58.
- @E-mail: phyooos@163.com
*/
public interface OnRecycleItemListener {
void OnRecycleItemClick(View v,Object o);
}
我發(fā)現(xiàn)Object o每一次都需要轉(zhuǎn)化,很麻煩也可以這樣使用,
public interface OnRecycleItemListener <T>{
void OnRecycleItemClick(View v,T o);
}
PS;參數(shù)什么的自己定義即可,用什么就定義什么
2)在onBindViewHolder給要添加的view設(shè)置點擊事件
@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并在其中調(diào)用接口中的OnRecycleItemClick
3)那么上面listener對象需要定義出來
private OnRecycleItemListener listener;
4)最后一步就是定義一個對外開放的方法
public void addRecycleItemListener(OnRecycleItemListener listener){
this.listener = listener;
}```
到此為止,你可以在activity中給創(chuàng)建出來的adapter實例添加點擊事件,并在回調(diào)中處理各種事件
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;
}
}
};```