将 onClickListener 设置为 onBindViewHolder 中的整个项目
Setting onClickListener to the whole item in onBindViewHolder
我有一个 RecyclerView Adapter
,我想将 onClick
设置为整个项目,但是这段代码以及当我也尝试使用 myViewHolder.view.setOnClickListener(..)
时,两者仍然只听到点击复选框。
如何将 onClickListener
设置为整个 item
?
public class DeliveryPointSelectionAdapter extends RecyclerView.Adapter<DeliveryPointSelectionAdapter.MyViewHolder> {
private static final String TAG = MyViewHolder.class.getSimpleName();
private List<DeliveryPoint> deliveryPointList;
interface OnItemCheckListener {
void onItemCheck(DeliveryPoint deliveryPoint);
void onItemUncheck(DeliveryPoint deliveryPoint);
}
@NonNull
private OnItemCheckListener onItemCheckListener;
public DeliveryPointSelectionAdapter(List<DeliveryPoint> deliveryPointList, @NonNull OnItemCheckListener onItemCheckListener) {
this.deliveryPointList = deliveryPointList;
this.onItemCheckListener = onItemCheckListener;
}
@NonNull
@Override
public DeliveryPointSelectionAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dialog_select_deliverypoints_to_change_item, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
DeliveryPoint deliveryPoint = deliveryPointList.get(i);
myViewHolder.tvFormattedString.setText(deliveryPoint.domesticAddress.formattedString);
myViewHolder.checkBox.setChecked(deliveryPoint.isCheckedForLocationUpdate);
myViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeliveryPoint deliveryPoint1 = deliveryPointList.get(myViewHolder.getAdapterPosition());
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint1.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint1);
} else {
deliveryPoint1.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint1);
}
}
});
// myViewHolder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
// DeliveryPoint deliveryPoint1 = deliveryPointList.get(myViewHolder.getAdapterPosition());
// if (myViewHolder.checkBox.isChecked()) {
// deliveryPoint1.setCheckedForLocationUpdate(true);
// onItemCheckListener.onItemCheck(deliveryPoint1);
// } else {
// deliveryPoint1.setCheckedForLocationUpdate(false);
// onItemCheckListener.onItemUncheck(deliveryPoint1);
// }
// });
}
@Override
public int getItemCount() {
return deliveryPointList.size();
}
@Override
public int getItemViewType(int position){
return position;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvFormattedString;
CheckBox checkBox;
LinearLayout linearLayout;
View view;
MyViewHolder(View view) {
super(view);
tvFormattedString = (TextView) view.findViewById(R.id.tv_formatted_string);
checkBox = view.findViewById(R.id.cb_deliverypoint_checked);
linearLayout = view.findViewById(R.id.ll_delivery_points_layout);
this.view = view;
}
}
}
项目视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_delivery_points_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/cb_deliverypoint_checked"/>
<TextView
android:id="@+id/tv_formatted_string"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingStart="25dp"
android:paddingTop="15dp"
android:paddingEnd="25dp"
android:textColor="@color/gray_text_light"
android:textSize="18sp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>
像其他用户已经提到的那样myViewHolder.itemView.setOnClickListener
在 recyclerView holder 上设置 onClick。
但是,如果我错了,请纠正我,您似乎希望您的复选框 checked/unchecked 只有持有者点击。在这种情况下,您需要通过在布局中添加以下代码来禁用它 xml
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
否则点击复选框将点击并不会执行您为 itemview
点击编写的代码。
由于您将其设置为基于 deliveryPoint.isCheckedForLocationUpdate
选中,因此您可以 notifyItemChanged(position)
在单击后更新持有人复选框。
看看这是否有帮助。
编辑:
根据评论,如果你真的想在视图中切换你的复选框,你应该使用这样的东西:同样,如果你创建一个新对象 deliveryPoint1
没有对视图中的原始对象进行任何更改
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint);
} else {
deliveryPoint.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint);
}
}
});
首先像这样创建一个 OnItemClickListener 变量:
public class DeliveryPointSelectionAdapter extends RecyclerView.Adapter<DeliveryPointSelectionAdapter.MyViewHolder> {
private OnItemClickListener listener;
然后朝向class代码的底部,实现setOnItemClickListener
方法
public void setOnItemClickListener(OnItemClickListener
listener){
this.listener = listener;
}
另外(在您的 class 代码末尾)您应该创建一个新界面来描述点击操作将生成的数据。像这样的事情:
public interface OnItemClickListener{
void OnItemClick(
int position);
}
然后尝试使用您在问题中描述的方法设置点击监听器 myViewHolder.view.setOnClickListener(..)
但是在 MyViewHolder(View view)
方法里面。
像这样:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if(position !=RecyclerView.NO_POSITION&& listener
!=null){
listener.OnItemClick(position);
}
您正在尝试选中复选框,当它现在被选中时,当它未被选中时取消选中。通过以下方式更改条件:
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint1.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint1);
} else {
deliveryPoint1.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint1);
}
notifyItemChanged(myViewHolder.getAdapterPosition());
并且记得在您的接口方法中调用 notifyDataSetChanged();
您的适配器。
我有一个 RecyclerView Adapter
,我想将 onClick
设置为整个项目,但是这段代码以及当我也尝试使用 myViewHolder.view.setOnClickListener(..)
时,两者仍然只听到点击复选框。
如何将 onClickListener
设置为整个 item
?
public class DeliveryPointSelectionAdapter extends RecyclerView.Adapter<DeliveryPointSelectionAdapter.MyViewHolder> {
private static final String TAG = MyViewHolder.class.getSimpleName();
private List<DeliveryPoint> deliveryPointList;
interface OnItemCheckListener {
void onItemCheck(DeliveryPoint deliveryPoint);
void onItemUncheck(DeliveryPoint deliveryPoint);
}
@NonNull
private OnItemCheckListener onItemCheckListener;
public DeliveryPointSelectionAdapter(List<DeliveryPoint> deliveryPointList, @NonNull OnItemCheckListener onItemCheckListener) {
this.deliveryPointList = deliveryPointList;
this.onItemCheckListener = onItemCheckListener;
}
@NonNull
@Override
public DeliveryPointSelectionAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dialog_select_deliverypoints_to_change_item, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
DeliveryPoint deliveryPoint = deliveryPointList.get(i);
myViewHolder.tvFormattedString.setText(deliveryPoint.domesticAddress.formattedString);
myViewHolder.checkBox.setChecked(deliveryPoint.isCheckedForLocationUpdate);
myViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeliveryPoint deliveryPoint1 = deliveryPointList.get(myViewHolder.getAdapterPosition());
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint1.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint1);
} else {
deliveryPoint1.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint1);
}
}
});
// myViewHolder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
// DeliveryPoint deliveryPoint1 = deliveryPointList.get(myViewHolder.getAdapterPosition());
// if (myViewHolder.checkBox.isChecked()) {
// deliveryPoint1.setCheckedForLocationUpdate(true);
// onItemCheckListener.onItemCheck(deliveryPoint1);
// } else {
// deliveryPoint1.setCheckedForLocationUpdate(false);
// onItemCheckListener.onItemUncheck(deliveryPoint1);
// }
// });
}
@Override
public int getItemCount() {
return deliveryPointList.size();
}
@Override
public int getItemViewType(int position){
return position;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvFormattedString;
CheckBox checkBox;
LinearLayout linearLayout;
View view;
MyViewHolder(View view) {
super(view);
tvFormattedString = (TextView) view.findViewById(R.id.tv_formatted_string);
checkBox = view.findViewById(R.id.cb_deliverypoint_checked);
linearLayout = view.findViewById(R.id.ll_delivery_points_layout);
this.view = view;
}
}
}
项目视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_delivery_points_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/cb_deliverypoint_checked"/>
<TextView
android:id="@+id/tv_formatted_string"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingStart="25dp"
android:paddingTop="15dp"
android:paddingEnd="25dp"
android:textColor="@color/gray_text_light"
android:textSize="18sp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>
像其他用户已经提到的那样myViewHolder.itemView.setOnClickListener
在 recyclerView holder 上设置 onClick。
但是,如果我错了,请纠正我,您似乎希望您的复选框 checked/unchecked 只有持有者点击。在这种情况下,您需要通过在布局中添加以下代码来禁用它 xml
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
否则点击复选框将点击并不会执行您为 itemview
点击编写的代码。
由于您将其设置为基于 deliveryPoint.isCheckedForLocationUpdate
选中,因此您可以 notifyItemChanged(position)
在单击后更新持有人复选框。
看看这是否有帮助。
编辑:
根据评论,如果你真的想在视图中切换你的复选框,你应该使用这样的东西:同样,如果你创建一个新对象 deliveryPoint1
没有对视图中的原始对象进行任何更改
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint);
} else {
deliveryPoint.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint);
}
}
});
首先像这样创建一个 OnItemClickListener 变量:
public class DeliveryPointSelectionAdapter extends RecyclerView.Adapter<DeliveryPointSelectionAdapter.MyViewHolder> {
private OnItemClickListener listener;
然后朝向class代码的底部,实现setOnItemClickListener
方法
public void setOnItemClickListener(OnItemClickListener
listener){
this.listener = listener;
}
另外(在您的 class 代码末尾)您应该创建一个新界面来描述点击操作将生成的数据。像这样的事情:
public interface OnItemClickListener{
void OnItemClick(
int position);
}
然后尝试使用您在问题中描述的方法设置点击监听器 myViewHolder.view.setOnClickListener(..)
但是在 MyViewHolder(View view)
方法里面。
像这样:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if(position !=RecyclerView.NO_POSITION&& listener
!=null){
listener.OnItemClick(position);
}
您正在尝试选中复选框,当它现在被选中时,当它未被选中时取消选中。通过以下方式更改条件:
if (myViewHolder.checkBox.isChecked()) {
deliveryPoint1.setCheckedForLocationUpdate(true);
onItemCheckListener.onItemCheck(deliveryPoint1);
} else {
deliveryPoint1.setCheckedForLocationUpdate(false);
onItemCheckListener.onItemUncheck(deliveryPoint1);
}
notifyItemChanged(myViewHolder.getAdapterPosition());
并且记得在您的接口方法中调用 notifyDataSetChanged();
您的适配器。