本文介绍: 如下图所示,在对RecyclerView中item及其多个子组件都需要不同的点击事件监听处理,需要使用到adapter的OnItemClickListener和OnItemChildClickListener两个对象。
第二步、在activity的布局中引入 RecyclerView
背景
如下图所示,在对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.LinearLayoutCompat
android: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.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android: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"/>
<ImageView
android: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"/>
<TextView
android: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.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/black_item_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="15sp"
android:text="中国-广东省-深圳市"/>
<TextView
android: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>
<Button
android: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.RecyclerView
android: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
@Override
public 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;
}
@Override
public 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());
}
@Override
public 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;
@Override
protected 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);
// 子控件1
btn_black_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemChildClickListener != null) {
onItemChildClickListener.onItemChildClick(view, getAdapterPosition(), "chil_1");
}
}
});
// 子控件2
iv_copy_ip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemChildClickListener != null) {
onItemChildClickListener.onItemChildClick(view, getAdapterPosition(), "chil_2");
}
}
});
// item点击监听
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public 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;
@Override
protected 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() {
@Override
public 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() {
@Override
public void onItemClick(int position) {
// item点击后执行...
}
});
}
}
原文地址:https://blog.csdn.net/weixin_42966151/article/details/134694046
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24624.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。