RecyclerView 的每个Item的点击事件并没有像 ListView 一样封装在组件中,需要 Item 的单击事件时就需要自己去实现,在 Adapter 中为RecyclerView 添加单击
第一步:
/**
* 定义 RecyclerView 选项单击事件的回调接口
*/
public interface OnItemClickListener{
void onItemClick(RecyclerView parent,View view,int position,String data);
}
第二步:
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
第三步:
public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{
...
view.setOnClickListener(this);
...
@Override
public void onClick(View view) {
int position = recyclerView.getChildAdapterPosition(view);
if (onItemClickListener!=null){
onItemClickListener.onItemClick(recyclerView,view,position,mList.get(position));
}
}
...
}
第四步:
adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent,String data) {
Toast.makeText(MainActivity.this,data,Toast.LENGTH_SHORT).show();
}
});
参考代码
Adapter
/**
* Created by jzman on 2017/5/13 0013.
* RecycleView的Adapter
*/
public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{
private Context mContext;
private RecyclerView recyclerView;
private ArrayList<String> mList;
public RvAdapter1() {}
public RvAdapter1(Context mContext,ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
/**
* 用于创建ViewHolder
* @param parent
* @param viewType
* @return
*/
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
view.setOnClickListener(this);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
/**
* 绑定数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(DataViewHolder holder,int position) {
holder.tv_data.setText(mList.get(position));
}
/**
* 选项总数
* @return
*/
@Override
public int getItemCount() {
return mList.size();
}
@Override
public void onClick(View view) {
int position = recyclerView.getChildAdapterPosition(view);
if (onItemClickListener!=null){
onItemClickListener.onItemClick(recyclerView,mList.get(position));
}
}
/**
* 创建ViewHolder
*/
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
/**
* 定义RecyclerView选项单击事件的回调接口
*/
public interface OnItemClickListener{
void onItemClick(RecyclerView parent,String data);
}
/**
* 将RecycleView附加到Adapter上
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView= recyclerView;
}
/**
* 将RecycleView从Adapter解除
*/
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
}
MainActivity
/**
* Created by jzman on 2017/5/13 0013.
*/
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
RvAdapter1 adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(this,4));
adapter = new RvAdapter1(this,initData());
adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent,String data) {
Toast.makeText(MainActivity.this,Toast.LENGTH_SHORT).show();
}
});
rv.setAdapter(adapter);
}
public static ArrayList<String> initData(){
ArrayList<String> arrayList = new ArrayList<>();
for (int i=0;i<50;i++){
arrayList.add("第"+i+"条数据");
}
return arrayList;
}
}
显示效果