为什么 setOnClickListener 不能在带有 recyclerview 的适配器中工作?

and*_*pie 1 android onclicklistener android-recyclerview

我只是简单地实现 setOnClickListener 来适配同一天前正在工作的 setOnClickListener 并且

几天前我遇到了同样的问题,但我做了另一个适配器,它成功了,但今天我又遇到了同样的问题

虽然我用谷歌搜索但确实得到了答案,我在这里发布了一个问题,可能是我经常遇到错误的原因,并记住这一点以获取进一步的代码。

适配器类

 package "";

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.Youtubedownloader.R;
import com.android.Youtubedownloader.activities.CustomPlayer;
import com.android.Youtubedownloader.database.Model;
import com.tonyodev.fetch.Fetch;
import com.tonyodev.fetch.request.RequestInfo;

import java.util.List;

import static com.android.Youtubedownloader.service.AppService.fetch;



public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.Holder> {

    private List<Model> list;

    public ViewAdapter(List<Model> list) {
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        final Model model = list.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.getStatus().equalsIgnoreCase("done")) {
                    if (!model.getFilePath().equals("") && model.getFilePath() != null) {
                       /* Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getFilePath()));
                        intent.setDataAndType(Uri.parse(model.getFilePath()), "video*//*");*/
                        Intent intent = new Intent(holder.itemView.getContext(), CustomPlayer.class);
                        intent.putExtra("path", model.getFilePath());
                        holder.itemView.getContext().startActivity(intent);
                    }
                } else {
                    Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();

                }
            }
        });




    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        TextView title, percentage, downloaded;
        ProgressBar progressBar;
        ImageView pause, resume;
        LinearLayout bottom;
        View itemView;

        //   TextView textView;

        public Holder(final View itemView) {
            super(itemView);
            this.itemView = itemView;
            //   textView = (TextView) itemView.findViewById(R.id.textview);
            title = (TextView) itemView.findViewById(R.id.video_title);
            percentage = (TextView) itemView.findViewById(R.id.txt_percentage);
            downloaded = (TextView) itemView.findViewById(R.id.txt_downloaded);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
            pause = (ImageView) itemView.findViewById(R.id.img_pause);
            resume = (ImageView) itemView.findViewById(R.id.img_resume);
            bottom = (LinearLayout) itemView.findViewById(R.id.bottom);
        }
    }  
}
Run Code Online (Sandbox Code Playgroud)

适配器项 xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        app:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/img_app"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="@dimen/margin_5">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/ic_download"
                    android:tint="@color/youtube" />

                <TextView
                    android:id="@+id/txt_percentage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="2dp"
                    android:gravity="center_vertical"
                    android:padding="2dp"
                    android:text="0%" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="10dp"
                android:layout_toLeftOf="@+id/bottom"

                android:layout_toRightOf="@+id/img_app"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/video_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:lines="1"
                    android:text="video title" />

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:progress="100" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/txt_downloaded"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:lines="1"
                        android:text="downloaded:" />


                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/img_pause"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="5dp"
                    android:background="@color/youtube"
                    android:clickable="true"
                    android:padding="15dp"
                    android:src="@mipmap/ic_pause_circle_filled_white_24dp"
                    android:tint="@color/white"
                    android:visibility="visible" />

                <ImageView
                    android:id="@+id/img_resume"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="5dp"
                    android:background="@color/youtube"
                    android:clickable="true"
                    android:padding="15dp"
                    android:src="@mipmap/ic_play_circle_filled_white_24dp"
                    android:tint="@color/white"
                    android:visibility="gone" />
            </LinearLayout>


        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

更新

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("click", "click working");
            Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();
            if (model.getStatus().equalsIgnoreCase("done")) {
                if (!model.getFilePath().equals("") && model.getFilePath() != null) {
                   /* Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getFilePath()));
                    intent.setDataAndType(Uri.parse(model.getFilePath()), "video*//*");*/
                    Intent intent = new Intent(holder.itemView.getContext(), CustomPlayer.class);
                    intent.putExtra("path", model.getFilePath());
                    holder.itemView.getContext().startActivity(intent);
                }
            } else {
                Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();

            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

更新xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Erc*_*can 15

我有同样的问题。经过多次尝试,我注意到 xml 属性以某种方式禁用了 ItemView clickListener。所以我从布局中删除了以下代码,现在它可以工作了。

android:clickable="true"
android:focusable="true"
Run Code Online (Sandbox Code Playgroud)

根据我的经验,这绝对有效。