双击Recycler View适配器的OnClickListener

时间:2015-06-23 00:28:00

标签: android onclicklistener android-recyclerview recycler-adapter

我正在尝试将简单的点击视图添加到回收站视图的项目中,但由于某种原因,我必须单击一次项目而不是一次以执行操作。单击时,回收器View似乎没有检测到点击。然而,在下一个上它会检测到点击并执行适当的操作。

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>

CODE:

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

        }

    }

我似乎无法找到导致此问题的问题。我可以得到任何帮助。感谢。

2 个答案:

答案 0 :(得分:14)

所以我发现了这个问题。以下两个标签是这里的罪魁祸首

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

当我们设置focusable和focusableInTouchMode = true时,基本上意味着您启用视图以首先关注触摸然后能够点击。

答案 1 :(得分:3)

我遇到了类似的问题,但是将焦点设置为false的解决方案在我的情况下不起作用。

相反,我的问题是我在持有我的Recycler视图的活动中设置了onItemTouch监听器。

USE [OperationsView]

DECLARE @ProductionDate  As DateTime
DECLARE @tag_in As VARCHAR(80)
DECLARE @FCChemGALPERMIN float
DECLARE @pi_server As VARCHAR(32)
DECLARE @endDate As DateTime
DECLARE @Debug As Bit
DECLARE @result As Float

SELECT @ProductionDate = '2016-12-01 07:00:00'
SELECT @tag_in = 'I-FC-835'
SELECT @pi_server = 'valpi'
SELECT @endDate = DATEADD(DAY, 1, @productionDate)
SELECT @Debug = 1

EXEC @FCChemGALPERMIN = Interface.proc_GetPIValueAverageTime 
           @result, @tag_in, @ProductionDate, @endDate, @pi_server, @Debug 
 PRINT @FCChemGALPERMIN   
 PRINT 'done'

通过从我的活动中删除它,我的其他触摸听众开始响应单击而不仅仅是双倍。