简体   繁体   English

回收站视图中的行项目不响应单击事件

[英]Row item in recycler view does not respond to click event

Following is the layout of my row item:以下是我的行项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">


    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_query"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="left|top"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/ic_anoun_red" />

        <TextView
            android:id="@+id/txt_ques_sub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginTop="12dp"
            android:layout_toRightOf="@+id/img_query"
            android:text="Problem in Merge sort"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light"
            android:textStyle="bold" />



        <TextView

            android:id="@+id/txt_ques_detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="2dp"
            android:layout_below="@+id/img_query"
            android:text="@string/sample_query_detail"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light" />

        <TextView
            android:id="@+id/txt_asked_by"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_detail"
            android:layout_marginRight="4dp"
            android:layout_marginTop="2dp"
            android:text="Tabish Butt"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/lightorange" />

        <TextView
            android:id="@+id/txt_ques_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_asked_by"
            android:layout_marginRight="4dp"
            android:text="07:15 am"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_ques_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_time"
            android:layout_marginRight="4dp"
            android:text="25 SEP 2015"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_replies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txt_ques_date"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="4dp"
            android:text="@string/default_num_answers"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/primary_dark_material_light" />


        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_below="@+id/txt_replies"
            android:alpha="0.12"
            android:background="@android:color/black" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_answer"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:drawableLeft="@android:drawable/ic_media_play"
                android:text="ANSWER"
                android:textColor="@color/primary_dark_material_light" />
        </LinearLayout>
    </RelativeLayout>
    </LinearLayout>

</LinearLayout>

Following is the onClick code:以下是点击代码:

holder.setClickListener(new MyViewHolder.ClickListener() {
            @Override
            public void onClick(View v, int position, boolean isLongClick) {
                if(isLongClick)
                {
                    /* TASK TO PERFORM AT LONG CLICKS */
                    Toast.makeText(context, "Long click acheived", Toast.LENGTH_LONG).show();
                }
                else
                {
                    /* TASK TO PERFORM AT CLICKS */
                    Toast.makeText(context, "Click Event", Toast.LENGTH_LONG).show();
                }
            }
        });

Now when i run this everything works fine except that clicking a row item does not generate any response (toasts in this case)现在,当我运行此程序时,一切正常,只是单击行项目不会产生任何响应(在这种情况下是敬酒)

any help will be greatly appreciated.任何帮助将不胜感激。 Need help !需要帮忙 !

Following is the method through which click events are handled in MyViewHolder class:以下是在MyViewHolder类中处理点击事件的方法:

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

    //Following represent grid_layout item
    TextView txtFileName;
    TextView txtFileIssueTime;
    Button btnAnswer;
    private ClickListener clickListener;
    public MyViewHolder(View itemView) {
        super(itemView);

        //VIEWS IN LAYOUT BEING INITIALIZED

        btnAnswer = (Button) itemView.findViewById(R.id.btn_answer);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    /* Interface for handling clicks - both normal and long ones. */
    public interface ClickListener {

        /**
         * Called when the view is clicked.
         *
         * @param v view that is clicked
         * @param position of the clicked item
         * @param isLongClick true if long click, false otherwise
         */
        public void onClick(View v, int position, boolean isLongClick);

    }

    /* Setter for listener. */
    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onClick(View v) {

        // If not long clicked, pass last variable as false.
        clickListener.onClick(v, getAdapterPosition(), false);
    }

    @Override
    public boolean onLongClick(View v) {

        // If long clicked, passed last variable as true.
        clickListener.onClick(v, getAdapterPosition(), true);
        return true;
    }

}

There's no need to set the click listeners to the itemView, you're already implementing the clicklisteners in the ViewHolder.无需为 itemView 设置点击侦听器,您已经在 ViewHolder 中实现了点击侦听器。

I am not sure still from where you're calling the "holder.setClickListener(...)", but assuming you're doing so from an Activity, I would rather follow this approach:我不确定您从哪里调用“holder.setClickListener(...)”,但假设您是从 Activity 调用的,我宁愿遵循以下方法:

1) Have the ClickListener as an independent interface (outside your holder), you could use it in a different adapter later. 1) 将 ClickListener 作为独立接口(在您的持有者之外),您可以稍后在不同的适配器中使用它。

2) Add a ClickListener to both your adapter and your ViewHolder. 2) 将 ClickListener 添加到您的适配器和 ViewHolder。

3) When setting up the Adapter, pass your Activity/Fragment as the listener (they should implement the ClickListener interface) 3) 设置适配器时,将您的 Activity/Fragment 作为侦听器传递(它们应该实现 ClickListener 接口)

4) When creating the ViewHolder, pass the listener as well to the ViewHolder and set it to the variable (that you already have defined). 4) 创建 ViewHolder 时,将侦听器也传递给 ViewHolder 并将其设置为变量(您已经定义)。

5) As you already do, in the onClick methods, call the clickListener methods. 5) 正如您已经做的那样,在 onClick 方法中,调用 clickListener 方法。

Let me know if that works.让我知道这是否有效。 It's exactly what I often do.这正是我经常做的。

Try moving the clickable to be in the parent LinearLayout , not the nested LinearLayout尝试将clickable移动到父LinearLayout ,而不是嵌套的LinearLayout

Edit:编辑:

actually, your layout should just be实际上,您的布局应该是

<FrameLayout
    android:background:"@color/white"
    android:foreground:"?selectableItemBackground">

    <RelativeLayout />

</FrameLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM