Android: 如何从 LongClick 方法中排除 CardView 的复选框?
Android: how do I exclude a CardView's checkbox from a LongClick method?
我在 ReyclerView 列表中有 CardView。我进行了设置,以便 CardView 上的 LongClick 启动 DialogFragment。 CardView 上有一个复选框,我想从 LongClick 方法中排除该复选框;也就是说,如果复选框被长按,我不想加载 DialogFragment。我将 'android:longClickable="false"' 和 'android:focusable="false"' 添加到 CardView 布局,但这没有用。我将如何实现?
Adapter file:
...
public static class ListViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
CardView singleCardView;
TextView cardBlankText1;
TextView cardBlankTextNumstotal;
CheckBox chkSelected;
TextView cardBlankText2;
TextView cardBlankText4;
TextView cardBlankText5A;
TextView cardBlankText5B;
TextView cardBlankText6;
TextView cardBlankText7;
TextView cardBlankText8;
public ListViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
singleCardView = (CardView) itemView.findViewById(R.id.singlecard_view1);
chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
cardBlankText1 = (TextView) itemView.findViewById(R.id.cardBlankText1);
cardBlankTextNumstotal = (TextView) itemView.findViewById(R.id.cardBlankTextNumstotal);
cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
cardBlankText4 = (TextView) itemView.findViewById(R.id.cardBlankText4);
cardBlankText5A = (TextView) itemView.findViewById(R.id.cardBlankText5A);
cardBlankText5B = (TextView) itemView.findViewById(R.id.cardBlankText5B);
cardBlankText6 = (TextView) itemView.findViewById(R.id.cardBlankText6);
cardBlankText7 = (TextView) itemView.findViewById(R.id.cardBlankText7);
cardBlankText8 = (TextView) itemView.findViewById(R.id.cardBlankText8);
chkSelected.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick (View view){
return true;
}
});
}
@Override
public boolean onLongClick(View view) {
clickListener.onItemLongClick(getAdapterPosition(), view);
return false;
}
}
public void setonItemClickListener(ClickListener clickListener) {
ListAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemLongClick(int position, View view);
}
获取复选框并在其上设置一个空的 OnLongClickListener。这将消耗事件并阻止父 CardView 获取它:
View checkbox = container.findViewById(R.id.checkbox);
checkbox.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//make sure to return true here so the parent cardview doesn't get the event.
return true;
}
});
我在 ReyclerView 列表中有 CardView。我进行了设置,以便 CardView 上的 LongClick 启动 DialogFragment。 CardView 上有一个复选框,我想从 LongClick 方法中排除该复选框;也就是说,如果复选框被长按,我不想加载 DialogFragment。我将 'android:longClickable="false"' 和 'android:focusable="false"' 添加到 CardView 布局,但这没有用。我将如何实现?
Adapter file:
...
public static class ListViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
CardView singleCardView;
TextView cardBlankText1;
TextView cardBlankTextNumstotal;
CheckBox chkSelected;
TextView cardBlankText2;
TextView cardBlankText4;
TextView cardBlankText5A;
TextView cardBlankText5B;
TextView cardBlankText6;
TextView cardBlankText7;
TextView cardBlankText8;
public ListViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
singleCardView = (CardView) itemView.findViewById(R.id.singlecard_view1);
chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
cardBlankText1 = (TextView) itemView.findViewById(R.id.cardBlankText1);
cardBlankTextNumstotal = (TextView) itemView.findViewById(R.id.cardBlankTextNumstotal);
cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
cardBlankText4 = (TextView) itemView.findViewById(R.id.cardBlankText4);
cardBlankText5A = (TextView) itemView.findViewById(R.id.cardBlankText5A);
cardBlankText5B = (TextView) itemView.findViewById(R.id.cardBlankText5B);
cardBlankText6 = (TextView) itemView.findViewById(R.id.cardBlankText6);
cardBlankText7 = (TextView) itemView.findViewById(R.id.cardBlankText7);
cardBlankText8 = (TextView) itemView.findViewById(R.id.cardBlankText8);
chkSelected.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick (View view){
return true;
}
});
}
@Override
public boolean onLongClick(View view) {
clickListener.onItemLongClick(getAdapterPosition(), view);
return false;
}
}
public void setonItemClickListener(ClickListener clickListener) {
ListAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemLongClick(int position, View view);
}
获取复选框并在其上设置一个空的 OnLongClickListener。这将消耗事件并阻止父 CardView 获取它:
View checkbox = container.findViewById(R.id.checkbox);
checkbox.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//make sure to return true here so the parent cardview doesn't get the event.
return true;
}
});