本文主要讲述在activity中回调RecyclerView 适配器中item的button点击事件。
(适配器也可以直接注册事件本文不再写)
item布局中的button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#1ac107"
android:id="@+id/send"
android:text="立即发送"
android:textColor="#FFFFFF"
android:textSize="18dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#e11534"
android:id="@+id/delete"
android:text="删除"
android:textColor="#FFFFFF"
android:textSize="18dp" />
</LinearLayout>
数据对象
public class zxbx_dfd {
private String wxdh, sbname, sbcode, address, content;
public zxbx_dfd(String wxdh, String sbname, String sbcode, String address, String content) {
this.wxdh = wxdh;
this.sbname = sbname;
this.sbcode = sbcode;
this.address = address;
this.content = content;
}
public String getWxdh() {
return wxdh;
}
public String getSbname() {
return sbname;
}
public String getSbcode() {
return sbcode;
}
public String getAddress() {
return address;
}
public String getContent() {
return content;
}
}
适配器
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> implements View.OnClickListener{
private List<zxbx_dfd> list;//数据源
private Context context;//上下文
public MyRecyclerViewAdapter(List<zxbx_dfd> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.zxbx_fd_item,parent,false);
return new MyViewHolder(view);
}
//绑定
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
zxbx_dfd data = list.get(position);
holder.wxdh.setText(data.getWxdh());
holder.sbname.setText(data.getSbname());
holder.sbcode.setText(data.getSbcode());
holder.address.setText(data.getAddress());
holder.content.setText(data.getContent());
holder.itemView.setTag(position);
holder.send.setTag(position);
holder.delete.setTag(position);
}
//有多少个item
@Override
public int getItemCount() {
return list.size();
}
//创建MyViewHolder继承RecyclerView.ViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView wxdh,sbname,sbcode,address,content;
private Button send,delete;
public MyViewHolder(View itemView) {
super(itemView);
wxdh = (TextView) itemView.findViewById(R.id.wxdh);
sbname = (TextView) itemView.findViewById(R.id.sbname);
sbcode = (TextView) itemView.findViewById(R.id.sbcode);
address = (TextView) itemView.findViewById(R.id.address);
content = (TextView) itemView.findViewById(R.id.content);
send = (Button) itemView.findViewById(R.id.send);
delete = (Button) itemView.findViewById(R.id.delete);
// 为ItemView添加点击事件
itemView.setOnClickListener(MyRecyclerViewAdapter.this);
send.setOnClickListener(MyRecyclerViewAdapter.this);
delete.setOnClickListener(MyRecyclerViewAdapter.this);
}
}
//=======================以下为item中的button控件点击事件处理===================================
//item里面有多个控件可以点击
public enum ViewName {
ITEM,
PRACTISE
}
//自定义一个回调接口来实现Click和LongClick事件
public interface OnItemClickListener {
void onItemClick(View v, ViewName viewName, int position);
void onItemLongClick(View v);
}
private OnItemClickListener mOnItemClickListener;//声明自定义的接口
//定义方法并暴露给外面的调用者
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public void onClick(View v) {
int position = (int) v.getTag(); //getTag()获取数据
if (mOnItemClickListener != null) {
switch (v.getId()){
case R.id.send://
mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
break;
default:
mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
break;
}
}
}
}
Activity实现 回调Button点击事情。
RecyclerView recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(fruitList,getActivity());
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, MyRecyclerViewAdapter.ViewName viewName, int position) {
switch (v.getId()){
case R.id.send:
Toast.makeText(getActivity(),"你点击了发送按钮"+(position+1),Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(getActivity(),"你点击了删除按钮"+(position+1),Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(),"你点击了item"+(position+1),Toast.LENGTH_SHORT).show();
break;
}
}