目录
背景
实现RecyclerView
第一步、 新建item的xml
第二步、在activity的布局中引入 RecyclerView
第三步、新建一个adapter
第四步、在activity中初始化绑定adapter即可
实现item及其多个子组件点击事件监听
第一步、 适配器中创建监听对象
第二步、适配器中绑定监听item和子组件
第三点、在activity中实现方法
背景
如下图所示,在对RecyclerView中item及其多个子组件都需要不同的点击事件监听处理,需要使用到adapter的OnItemClickListener和OnItemChildClickListener两个对象。
实现RecyclerView
第一步、 新建item的xml
新建一个xml文件(black_item.xml)作为单个item的样式,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"><androidx.appcompat.widget.LinearLayoutCompatandroid:id="@+id/lc_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center_vertical"android:layout_gravity="center_vertical"android:layout_marginLeft="5dp"tools:ignore="MissingConstraints"><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/black_item_ip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textStyle="bold"android:text="180.123.121.118"/><ImageViewandroid:id="@+id/iv_copy_ip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:src="@drawable/baseline_content_copy_24"/><TextViewandroid:id="@+id/black_item_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="right"android:textSize="12sp"android:textColor="@color/notice_item_time"android:text="2023-11-06 12:10:11"/></androidx.appcompat.widget.LinearLayoutCompat><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/black_item_address"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:textSize="15sp"android:text="中国-广东省-深圳市"/><TextViewandroid:id="@+id/black_item_note"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:layout_marginBottom="2dp"android:textSize="12sp"android:textColor="@color/notice_item_time"android:text="备注:"/></androidx.appcompat.widget.LinearLayoutCompat><Buttonandroid:id="@+id/btn_black_del"android:layout_width="wrap_content"android:layout_height="30sp"android:background="@drawable/btn_black_del"android:layout_gravity="center_vertical"android:textColor="@color/white"android:text="删除"/></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat>
样式效果如下图:
第二步、在activity的布局中引入 RecyclerView
<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_black"tools:listitem="@layout/black_item"android:layout_width="match_parent"android:layout_height="match_parent" />
第三步、新建一个adapter
用于相应数据和功能配置,代码如下:
public class BlackRVAdapter extends RecyclerView.Adapter<BlackRVAdapter.MyHolder> {private List<BlackItem> myBlackList = new ArrayList<>();public void setListData (List<BlackItem> list) {this.myBlackList = list;notifyDataSetChanged(); // 刷新}@NonNull@Overridepublic MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {// 加载布局View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.black_item, parent, false);MyHolder myHolder = new MyHolder(view);return myHolder;}@Overridepublic void onBindViewHolder(@NonNull MyHolder holder, @SuppressLint("RecyclerView") int position) {// 绑定数据BlackItem BlackItem = myBlackList.get(position);// 修改item样式holder.black_address.setText(BlackItem.getIp_address());holder.black_note.setText("备注:" + BlackItem.getNote());holder.black_time.setText(BlackItem.getCreated_time());holder.black_ip.setText(BlackItem.getIp());}@Overridepublic int getItemCount() {return myBlackList.size();}static class MyHolder extends RecyclerView.ViewHolder {TextView black_address;TextView black_note;TextView black_time;TextView black_ip;ImageView iv_copy_ip;Button btn_black_del;public MyHolder(@NonNull View itemView) {super(itemView);black_time = itemView.findViewById(R.id.black_item_time);black_ip = itemView.findViewById(R.id.black_item_ip);black_address = itemView.findViewById(R.id.black_item_address);black_note = itemView.findViewById(R.id.black_item_note);iv_copy_ip = itemView.findViewById(R.id.iv_copy_ip);btn_black_del = itemView.findViewById(R.id.btn_black_del);}}
}
第四步、在activity中初始化绑定adapter即可
public class IpBlackListActivity extends AppCompatActivity {private RecyclerView blackRecyclerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ip_black_list);initView();}private void initView () {blackRecyclerView = findViewById(R.id.rv_black);// 绑定适配器blackRVAdapter = new BlackRVAdapter();blackRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));blackRecyclerView.setAdapter(blackRVAdapter);blackRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL)); // 添加下划线}
}
实现item及其多个子组件点击事件监听
第一步、 适配器中创建监听对象
public class BlackRVAdapter extends RecyclerView.Adapter<BlackRVAdapter.MyHolder> {private static OnItemChildClickListener onItemChildClickListener;private static OnItemClickListener onItemClickListener;public interface OnItemChildClickListener {/*** 子组件点击事件* @param view* @param position* @param type 子组件类型,用于判断是哪个子组件*/void onItemChildClick(View view, int position, String type);}public void setOnItemClickListener(OnItemChildClickListener listenser) {onItemChildClickListener = listenser;}public interface OnItemClickListener {void onItemClick(int position);}public void setOnItemClickListener(OnItemClickListener listenser) {onItemClickListener = listenser;}}
第二步、适配器中绑定监听item和子组件
public class BlackRVAdapter extends RecyclerView.Adapter<BlackRVAdapter.MyHolder> {private static OnItemChildClickListener onItemChildClickListener;private static OnItemClickListener onItemClickListener;static class MyHolder extends RecyclerView.ViewHolder {public MyHolder(@NonNull View itemView) {super(itemView);// 绑定子控件iv_copy_ip = itemView.findViewById(R.id.iv_copy_ip);btn_black_del = itemView.findViewById(R.id.btn_black_del);// 子控件1btn_black_del.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (onItemChildClickListener != null) {onItemChildClickListener.onItemChildClick(view, getAdapterPosition(), "chil_1");}}});// 子控件2iv_copy_ip.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (onItemChildClickListener != null) {onItemChildClickListener.onItemChildClick(view, getAdapterPosition(), "chil_2");}}});// item点击监听itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (onItemClickListener != null) {onItemClickListener.onItemClick(getAdapterPosition());}}});}}public interface OnItemChildClickListener {/*** 子组件点击事件* @param view* @param position* @param type 子组件类型,用于判断是哪个子组件*/void onItemChildClick(View view, int position, String type);}public void setOnItemClickListener(OnItemChildClickListener listenser) {onItemChildClickListener = listenser;}public interface OnItemClickListener {void onItemClick(int position);}public void setOnItemClickListener(OnItemClickListener listenser) {onItemClickListener = listenser;}}
第三点、在activity中实现方法
public class IpBlackListActivity extends AppCompatActivity {private RecyclerView blackRecyclerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ip_black_list);initView();}private void initView () {blackRecyclerView = findViewById(R.id.rv_black);// 绑定适配器blackRVAdapter = new BlackRVAdapter();blackRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));blackRecyclerView.setAdapter(blackRVAdapter);blackRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL)); // 添加下划线// 子组件点击事件blackRVAdapter.setOnItemClickListener(new BlackRVAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(View view, int position, String type) {if (type.equals("chil_1")) {// 子组件1点击后执行...} else if (type.equals("chil_2") {// 子组件2点击后执行...}}}// item点击事件blackRVAdapter.setOnItemClickListener(new BlackRVAdapter.OnItemClickListener() {@Overridepublic void onItemClick(int position) {// item点击后执行... }});}
}