gpt4 book ai didi

java - 如何实现Android Recyclerview项目点击和复选框项目点击?

转载 作者:行者123 更新时间:2023-12-01 18:00:38 25 4
gpt4 key购买 nike

我有以下有效的支架,即我能够选中/取消选中该复选框。 (该复选框是 RecyclerView 卡的一部分):

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.bindData(numbers.get(position));
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);

//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(numbers.get(position).isSelected());

holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
}
});
}

我想实现RecyclerView上项目的点击。所以,我借鉴了这个解决方案 - 请检查:https://stackoverflow.com/a/26196831/4013399

但是,现在,整个卡项的单击可以工作,但无法选中/取消选中复选框。怎么解决这个问题?

请帮忙。谢谢。

更新

我进行了更改 - 但是,“HERE-1,2 和 3”行从未输入。

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Log.e("I AM ", "HERE-0");
holder.bindData(numbers.get(position));

final RelativeLayout rlyItem = holder.rlyItem;
final CheckBox checkbox = holder.checkbox;

rlyItem.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
mListener.onItemClicked(holder.getLayoutPosition());
Log.e("I AM ", "HERE-1");
}
});

checkbox.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
checkbox.setChecked(false);
Log.e("I AM ", "HERE-2");
} else {
checkbox.setChecked(true);
Log.e("I AM ", "HERE-3");
}
// Inform to Activity or the Fragment where the RecyclerView reside.
mListener.onItemCheckBoxChecked(((CheckBox) view).isChecked(), holder.getLayoutPosition());
}
});
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);

//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(numbers.get(position).isSelected());

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
Log.e("I AM ", "HERE-4");
//Log.e(Integer.toString(holder.getAdapterPosition()), " IS CHECKED");
}
});

}

最佳答案

只需在单击适配器类中的适配器膨胀 View 时为复选框调用 performClick() 即可。

 holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.checkbox.performClick();
}
});

关于java - 如何实现Android Recyclerview项目点击和复选框项目点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41236528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com