目录
背景
实现RecyclerView
第一步、 新建item的xml
第二步、在activity的布局中引入 RecyclerView
第三步、新建一个adapter
第四步、在activity中初始化绑定adapter即可
实现item及其多个子组件点击事件监听
第一步、 适配器中创建监听对象
第二步、适配器中绑定监听item和子组件
第三点、在activity中实现方法
如下图所示,在对RecyclerView中item及其多个子组件都需要不同的点击事件监听处理,需要使用到adapter的OnItemClickListener和OnItemChildClickListener两个对象。
新建一个xml文件(black_item.xml)作为单个item的样式,代码如下:
样式效果如下图:
用于相应数据和功能配置,代码如下:
public class BlackRVAdapter extends RecyclerView.Adapter {
private List myBlackList = new ArrayList<>();
public void setListData (List 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);
}
}
}
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)); // 添加下划线
}
}
public class BlackRVAdapter extends RecyclerView.Adapter {
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;
}
}
public class BlackRVAdapter extends RecyclerView.Adapter {
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;
}
}
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点击后执行...
}
});
}
}