【问题标题】:why setOnClickListener not working in adapter with recyclerview?为什么 setOnClickListener 不能在带有 recyclerview 的适配器中工作?
【发布时间】:2017-04-28 06:29:40
【问题描述】:

我只是简单地实现 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);
        }
    }  
}

适配器项 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>

更新

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

            }
        }
    });

更新 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>

【问题讨论】:

  • 如果反对者解释一下我会更好'
  • 这段代码很好,onclick 监听器可以在这种情况下工作。请检查您的 if 条件。
  • @anddevmanu 但它不起作用
  • 在 if 条件之前和 if 条件之后添加日志,以便您知道结果
  • @anddevmanu 试过但没用

标签: android android-recyclerview onclicklistener


【解决方案1】:

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

android:clickable="true"
android:focusable="true"

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

【讨论】:

  • 天才。太奇怪了,Google SDK 文档没有指示这样做。
【解决方案2】:

试试下面的代码

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

private List<Model> list;
private Context mContext;//Init Context

public ViewAdapter(List<Model> list,Context _context) {
    this.list = list;
    this.mContext = _context;//getting your activity context
}

@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.mParent.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(mContext, "file not found", Toast.LENGTH_SHORT).show();//here your using context

            }
        }
    });




}

@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;
    CardView mParent;

    //   TextView textView;

    public Holder(final View itemView) {
        super(itemView);
        this.itemView = itemView;
        //   textView = (TextView) itemView.findViewById(R.id.textview);
        mParent = (CardView)itemView.findViewById(R.id.cardview);
        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);
    }
 }
}

【讨论】:

  • holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(holder.itemView.getContext(), "cardview", Toast.LENGTH_SHORT )。显示(); } });没有运气
  • 查看我编辑的代码,因为您没有获得使用适配器的活动上下文
  • 始终在适配器构造函数中传递活动上下文
  • ??突出变化
  • 你只需替换我的适配器代码,我会添加注释
【解决方案3】:

在适配器的构造函数中,传递 getActivity() 对我有用,而不是 getContex()

【讨论】:

    【解决方案4】:

    将更新 xml 从 RelativeLayout 更改为 LinearLayout

    【讨论】:

    【解决方案5】:

    试试这个代码。

    视图适配器

    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;
        private OnItemClickListener listener;
    
        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);
        }
    
        @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);
    
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                         int position = getAdapterPosition();
                         if (listener != null && position != RecyclerView.NO_POSITION) 
                         {
                             listener.OnItemClick(getItem(position));
                         }
                    }
                });
            }
            
        }  
        public interface OnItemClickListener {
           void OnItemClick(ChildActivityList item);
        }
    
        public void setOnItemClickListener(OnItemClickListener listener) {
           this.listener = listener;
        }
    }
    

    Activity.Class 在您的活动中,只需调用适配器和那里的所有 if-else 条件

    adater.setOnItemClickListener(new OnItemClickListener);
    

    【讨论】:

      【解决方案6】:

      同样,我在 2 个适配器中遇到此错误,但是当我从 XML 中删除以下 2 个内容时,我的 setOnClickListener 在适配器中工作正常。

      <com.balysv.materialripple.MaterialRippleLayout
      

      android:clickable="true"
      

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多