双击 Recycler View Adapter 的 OnClickListener
Double Click OnClickListener of a Recycler View Adapter
我正在尝试向回收站视图的项目添加一个简单的点击视图,但由于某种原因,我必须点击一个项目两次而不是一次才能执行操作。单击时,回收器视图似乎没有检测到单击。然而,在下一个上,它会检测到点击并执行适当的操作。
XML :
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/rlContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector_inventory_recycler_item"
android:padding="16dp">
<ImageView
android:id="@+id/item_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:id="@+id/txtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/item_photo"
android:textSize="16sp"
/>
<TextView
android:id="@+id/txtItemQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtItemName"
android:layout_toRightOf="@+id/item_photo"
/>
<TextView
android:id="@+id/txtItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtItemQuantity"
android:layout_toRightOf="@+id/item_photo"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
代码:
public class InventoryItemRecyclerAdapter extends RecyclerView.Adapter<InventoryItemRecyclerAdapter.InventoryItemViewHolder> {
onItemClickListener mOnItemClickListener = null;
/**
*
*/
public ArrayList<Product> mInventoryItemList;
Context mContext;
static String TAG = "InventoryItemRecyclerAdapter";
Random random = new Random();
// -------------------------------------------------------------------------
// Constructor
/**
*
* @param pInventoryItemList
*/
public InventoryItemRecyclerAdapter(ArrayList<Product> pInventoryItemList) {
mInventoryItemList = pInventoryItemList;
}
// ---------------------------------------------------------------------
@Override
public InventoryItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
// Inflate the Layout for an item
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_inventory_recycler_adapter, parent, false);
// Instantiate ViewHolder
InventoryItemViewHolder inventoryItemViewHolder = new InventoryItemViewHolder(v);
return inventoryItemViewHolder;
}
@Override
public void onBindViewHolder(InventoryItemViewHolder holder, int position) {
...
}
// ---------------------------------------------------------------------------------------------
/**
* Returns the total number of items in the data set hold by the adapter.
*
* @return The total number of items in this adapter.
*/
@Override
public int getItemCount() {
return mInventoryItemList.size();
}
// ---------------------------------------------------------------------------------------------
// View Holder
/**
* RecyclerView
*/
public class InventoryItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// -----------------------------------------------------------------------------------------
// Vars
public CardView cardView;
public RelativeLayout uiContainer;
public TextView productName;
public TextView productPrice;
public TextView productQuantity;
public ImageView productImage;
public Product mProduct;
// -----------------------------------------------------------------------------------------
// Constructor
public InventoryItemViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cardView);
productName = (TextView) itemView.findViewById(R.id.txtItemName);
productImage = (ImageView) itemView.findViewById(R.id.item_photo);
productPrice = (TextView) itemView.findViewById(R.id.txtItemPrice);
productQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantity);
uiContainer = (RelativeLayout) itemView.findViewById(R.id.rlContainer);
uiContainer.setOnClickListener(this);
}
// -----------------------------------------------------------------------------------------
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
Log.e("InventoryItemRecyclerAdapter", "onItemClick");
// Throw a null pointer exception if this is null
if (mOnItemClickListener == null) {
throw new NullPointerException("mOnItemClickListener is null in InventoryItemRecyclerAdapter");
}
// Delegate to its caller. Let it handle the work
mOnItemClickListener.onRecyclerViewItemClick(this);
}
// -------------------------------------------------------------
}
// -----------------------------------------------------------------
/**
* Interface for RecyclerView
*/
public interface onItemClickListener {
/**
*
* @param pItemViewHolder
*/
public void onRecyclerViewItemClick(InventoryItemRecyclerAdapter.InventoryItemViewHolder pItemViewHolder);
}
}
我似乎找不到导致此问题的问题。请问我能得到任何帮助吗?谢谢
所以我发现了问题。以下两个标签是这里的罪魁祸首
android:focusable="true"
android:focusableInTouchMode="true"
当我们设置 focusable 和 focusableInTouchMode = true 时,基本上这意味着您正在启用视图以首先关注触摸然后能够点击。
我遇到了类似的问题,但是将 focusable 设置为 false 的解决方案在我的情况下不起作用。
相反,我的问题是我在持有回收站视图的 activity 中设置了 onItemTouch 侦听器。
recyclerView.addOnItemTouchListener(...);
通过从我的 activity 中删除它,我的其他触摸监听器开始响应单次点击,而不仅仅是两次。
我正在尝试向回收站视图的项目添加一个简单的点击视图,但由于某种原因,我必须点击一个项目两次而不是一次才能执行操作。单击时,回收器视图似乎没有检测到单击。然而,在下一个上,它会检测到点击并执行适当的操作。
XML :
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/rlContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector_inventory_recycler_item"
android:padding="16dp">
<ImageView
android:id="@+id/item_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:id="@+id/txtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/item_photo"
android:textSize="16sp"
/>
<TextView
android:id="@+id/txtItemQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtItemName"
android:layout_toRightOf="@+id/item_photo"
/>
<TextView
android:id="@+id/txtItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtItemQuantity"
android:layout_toRightOf="@+id/item_photo"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
代码:
public class InventoryItemRecyclerAdapter extends RecyclerView.Adapter<InventoryItemRecyclerAdapter.InventoryItemViewHolder> {
onItemClickListener mOnItemClickListener = null;
/**
*
*/
public ArrayList<Product> mInventoryItemList;
Context mContext;
static String TAG = "InventoryItemRecyclerAdapter";
Random random = new Random();
// -------------------------------------------------------------------------
// Constructor
/**
*
* @param pInventoryItemList
*/
public InventoryItemRecyclerAdapter(ArrayList<Product> pInventoryItemList) {
mInventoryItemList = pInventoryItemList;
}
// ---------------------------------------------------------------------
@Override
public InventoryItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
// Inflate the Layout for an item
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_inventory_recycler_adapter, parent, false);
// Instantiate ViewHolder
InventoryItemViewHolder inventoryItemViewHolder = new InventoryItemViewHolder(v);
return inventoryItemViewHolder;
}
@Override
public void onBindViewHolder(InventoryItemViewHolder holder, int position) {
...
}
// ---------------------------------------------------------------------------------------------
/**
* Returns the total number of items in the data set hold by the adapter.
*
* @return The total number of items in this adapter.
*/
@Override
public int getItemCount() {
return mInventoryItemList.size();
}
// ---------------------------------------------------------------------------------------------
// View Holder
/**
* RecyclerView
*/
public class InventoryItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// -----------------------------------------------------------------------------------------
// Vars
public CardView cardView;
public RelativeLayout uiContainer;
public TextView productName;
public TextView productPrice;
public TextView productQuantity;
public ImageView productImage;
public Product mProduct;
// -----------------------------------------------------------------------------------------
// Constructor
public InventoryItemViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cardView);
productName = (TextView) itemView.findViewById(R.id.txtItemName);
productImage = (ImageView) itemView.findViewById(R.id.item_photo);
productPrice = (TextView) itemView.findViewById(R.id.txtItemPrice);
productQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantity);
uiContainer = (RelativeLayout) itemView.findViewById(R.id.rlContainer);
uiContainer.setOnClickListener(this);
}
// -----------------------------------------------------------------------------------------
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
Log.e("InventoryItemRecyclerAdapter", "onItemClick");
// Throw a null pointer exception if this is null
if (mOnItemClickListener == null) {
throw new NullPointerException("mOnItemClickListener is null in InventoryItemRecyclerAdapter");
}
// Delegate to its caller. Let it handle the work
mOnItemClickListener.onRecyclerViewItemClick(this);
}
// -------------------------------------------------------------
}
// -----------------------------------------------------------------
/**
* Interface for RecyclerView
*/
public interface onItemClickListener {
/**
*
* @param pItemViewHolder
*/
public void onRecyclerViewItemClick(InventoryItemRecyclerAdapter.InventoryItemViewHolder pItemViewHolder);
}
}
我似乎找不到导致此问题的问题。请问我能得到任何帮助吗?谢谢
所以我发现了问题。以下两个标签是这里的罪魁祸首
android:focusable="true"
android:focusableInTouchMode="true"
当我们设置 focusable 和 focusableInTouchMode = true 时,基本上这意味着您正在启用视图以首先关注触摸然后能够点击。
我遇到了类似的问题,但是将 focusable 设置为 false 的解决方案在我的情况下不起作用。
相反,我的问题是我在持有回收站视图的 activity 中设置了 onItemTouch 侦听器。
recyclerView.addOnItemTouchListener(...);
通过从我的 activity 中删除它,我的其他触摸监听器开始响应单次点击,而不仅仅是两次。