双击Recycler视图适配器的OnClickListener [英] Double Click OnClickListener of a Recycler View Adapter

查看:88
本文介绍了双击Recycler视图适配器的OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个简单的Click View添加到回收者视图的项目中,但是由于某种原因,我必须单击一个项目两次而不是一次来执行操作.单击时,似乎Recycler View未检测到单击.但是,在下一个上,它会检测到点击并执行适当的操作.

I am trying to add a simple Click View to an item of a recycler view, but for some reason I have to click on an item twice instead of once to perform an action. On single click it seems like that recycler View does not detect a click. On the next one however it detects the click and performs a suitable action.

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);

        }

    }

我似乎找不到导致此问题的问题.请给我任何帮助.谢谢.

I couldn't seem to find the problem thats causing this issue. Can I get any help on this please. Thanks.

推荐答案

所以我发现了问题.以下两个标签是罪魁祸首

So I found the issue. The following two tags were the culprits here

android:focusable="true"
android:focusableInTouchMode="true"

基本上,当我们将focusable和focusableInTouchMode设置为true时,这意味着您可以使视图首先专注于触摸,然后能够单击.

When we set focusable and focusableInTouchMode = true basically it means that you are enabling a view to first focus on a touch and then be able to click.

这篇关于双击Recycler视图适配器的OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆