Android 双击Recycler视图适配器的ClickListener
我试图将一个简单的单击视图添加到回收器视图的项目中,但由于某些原因,我必须在项目上单击两次而不是一次来执行操作。单击一次,回收器视图似乎没有检测到单击。但是,在下一次操作中,它会检测到咔哒声并执行适当的操作 XML:Android 双击Recycler视图适配器的ClickListener,android,onclicklistener,android-recyclerview,Android,Onclicklistener,Android Recyclerview,我试图将一个简单的单击视图添加到回收器视图的项目中,但由于某些原因,我必须在项目上单击两次而不是一次来执行操作。单击一次,回收器视图似乎没有检测到单击。但是,在下一次操作中,它会检测到咔哒声并执行适当的操作 XML: <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cardView" andr
<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>
代码:
公共类InventoryItemRecyclerAdapter扩展了RecyclerView.Adapter{
onItemClickListener MonitorItemClickListener=null;
/**
*
*/
公共ArrayList mInventoryItemList;
语境;
静态字符串TAG=“InventoryItemRecyclerAdapter”;
随机=新随机();
// -------------------------------------------------------------------------
//建造师
/**
*
*@param pInventoryItemList
*/
公共InventoryItemRecyclerAdapter(ArrayList pInventoryItemList){
mInventoryItemList=pInventoryItemList;
}
// ---------------------------------------------------------------------
@凌驾
公共InventoryItemViewHolder onCreateViewHolder(视图组父级,int-viewType){
mContext=parent.getContext();
//为项目的布局充气
视图v=LayoutInflater.from(parent.getContext())
.充气(R.layout.item\u inventory\u recycler\u adapter,parent,false);
//实例化ViewHolder
InventoryItemViewHolder InventoryItemViewHolder=新的InventoryItemViewHolder(v);
返回inventoryItemViewHolder;
}
@凌驾
BindViewHolder上的公共无效(InventoryItemViewHolder,int位置){
...
}
// ---------------------------------------------------------------------------------------------
/**
*返回适配器保留的数据集中的项目总数。
*
*@返回此适配器中的项目总数。
*/
@凌驾
public int getItemCount(){
返回mInventoryItemList.size();
}
// ---------------------------------------------------------------------------------------------
//视图持有者
/**
*回收视图
*/
公共类InventoryItemViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
// -----------------------------------------------------------------------------------------
//瓦尔斯
公共卡视图卡视图;
公共相对物uiContainer;
公共文本视图产品名称;
公共文本视图产品价格;
公共文本查看产品数量;
公众形象;产品形象;
公共产品;
// -----------------------------------------------------------------------------------------
//建造师
公共清单项目视图持有者(视图项目视图){
超级(项目视图);
cardView=(cardView)itemView.findViewById(R.id.cardView);
productName=(TextView)itemView.findViewById(R.id.txtItemName);
productImage=(ImageView)itemView.findViewById(R.id.item\u photo);
productPrice=(TextView)itemView.findViewById(R.id.txtItemPrice);
productQuantity=(TextView)itemView.findViewById(R.id.txtItemQuantity);
uiContainer=(RelativeLayout)itemView.findViewById(R.id.rlContainer);
uiContainer.setOnClickListener(这个);
}
// -----------------------------------------------------------------------------------------
/**
*单击视图时调用。
*
*@param v单击的视图。
*/
@凌驾
公共void onClick(视图v){
Log.e(“InventoryItemRecyclerAdapter”、“onItemClick”);
//如果为空,则引发空指针异常
如果(MonitorClickListener==null){
抛出新的NullPointerException(“MonitorItemClickListener在InventoryItemRecyclerAdapter中为null”);
}
//委托给它的调用方。让它处理工作
mOnItemClickListener.onRecycleServiceItemClick(此);
}
// -------------------------------------------------------------
}
// -----------------------------------------------------------------
/**
*RecyclerView接口
*/
公共接口侦听器{
/**
*
*@param pItemViewHolder
*/
RecycleServiceItemClick(InventoryItemRecyclerAdapter.InventoryItemViewHolder pItemViewHolder)上的公共作废;
}
}
我似乎找不到导致此问题的问题。请帮我一下好吗。谢谢。所以我找到了问题。以下两个标签是罪魁祸首
android:focusable="true"
android:focusableInTouchMode="true"
当我们将focusable和focusableInTouchMode设置为true时,基本上意味着您正在启用视图,使其首先聚焦于触摸,然后能够单击。我也有类似的问题,但将focusable设置为false的解决方案在我的情况下不起作用 相反,我的问题是我在保存我的recycler视图的活动中设置了onItemTouch侦听器
recyclerView.addOnItemTouchListener(...);
通过将其从我的活动中删除,我的其他触控监听器开始在单次点击而不是双次点击时做出响应。您好,请尝试以下操作:itemView.setOnClickListener(this);谢谢我已经试过了。但这不起作用。在这种情况下,项目视图是卡片视图。其余的组件都是在一个相对的布局上绘制的,我不太确定如何解决它:-/也许是on
recyclerView.addOnItemTouchListener(...);