简体   繁体   English

Recyclerview单个项目单击监听器

[英]Recyclerview individual item click listener

  • I have a recyclerview adapter on which I create listener to handle click event. 我有一个recyclerview适配器,可以在其上创建侦听器来处理click事件。
  • I implement this listener inside activity or fragment. 我在活动或片段中实现了此侦听器。 The recyclerview items contains lot of views inside cardview. recyclerview项包含cardview内部的许多视图。
  • My problem is when I click img_download no callback occur (the log not display) but when I click on other space (not img_download) the appropriate callback occur. 我的问题是,当我单击img_download时,没有发生回调(未显示日志),但是当我单击其他空间(而非img_download)时,发生了适当的回调。
  • How do I know which view is clicked ? 我如何知道单击了哪个视图?

Adapter Code: 适配器代码:

    public class ItuneEpisodeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tvTitle;
        TextView tvDescription;
        View divider;
        ImageView img_download;

        public ItuneEpisodeViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_podcast_title);
            tvDescription = itemView.findViewById(R.id.tv_description);
            divider = itemView.findViewById(R.id.divider);
            img_download = itemView.findViewById(R.id.img_download);
            itemView.setOnClickListener(this);
            img_download.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (view.getId() == img_download.getId()) {
                mOnClickListener.onDownloadItemClick();
            } else {
                mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
            }
        }
    }

    public interface EpisodeClickListener {
        void onItemClick(Episode podcast, View view);
        void onDownloadItemClick();
    }

Activity Implementation: 活动实施:

@Override
    public void onItemClick(Episode episode, View view) {
        PlayMediaActivity_.intent(this).extra("episode_extra", Parcels.wrap(episode)).extra("img", podcast.getCoverImage()).start();
    }

    @Override
    public void onDownloadItemClick() {
        Log.d("download", "yes");
    }

Screenshot: 屏幕截图:

在此处输入图片说明

XML code: XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:elevation="4dp">

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/md_blue_grey_100"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/img_podcast"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/podcast_img"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider" />

    <TextView
        android:id="@+id/tv_podcast_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text="Les chemins de la philosophie"
        android:textColor="@color/md_black_1000"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toRightOf="@+id/img_podcast"
        app:layout_constraintRight_toLeftOf="@+id/img_download"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:text="This is a description and should be replace with the best one let"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/tv_podcast_title"
        app:layout_constraintRight_toLeftOf="@+id/img_download"
        app:layout_constraintTop_toBottomOf="@+id/tv_podcast_title" />


    <ImageView
        android:id="@+id/img_calendar"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/ic_calendar"
        app:layout_constraintLeft_toLeftOf="@+id/tv_description"
        app:layout_constraintTop_toBottomOf="@+id/tv_description" />

    <TextView
        android:id="@+id/tv_calendar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Fév. 14 2016"
        app:layout_constraintBottom_toBottomOf="@+id/img_calendar"
        app:layout_constraintLeft_toRightOf="@+id/img_calendar" />

    <ImageView
        android:id="@+id/img_time"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_time"
        app:layout_constraintBottom_toBottomOf="@+id/tv_calendar"
        app:layout_constraintLeft_toRightOf="@+id/tv_calendar" />

    <TextView
        android:id="@+id/tv_realisateur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="49:50"
        app:layout_constraintBottom_toBottomOf="@+id/img_time"
        app:layout_constraintLeft_toRightOf="@+id/img_time" />

    <ImageView
        android:id="@+id/img_download"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_margin="5dp"
        android:tint="@color/primary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_drawer_download" />


</android.support.constraint.ConstraintLayout>

Solved it, add this two code on ConstraintLayout (parent layout) and on your img_download (ImageView) 解决后,将这两个代码添加到ConstraintLayout (父布局)和img_download (ImageView)上

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

and instead of itemView , put an ID on your ConstraintLayout and set an onClickListener on it. 而不是itemView ,在您的ConstraintLayout上放置一个ID并在其上设置onClickListener

    ConstraintLayout main;
    TextView tvTitle;
    TextView tvDescription;
    View divider;
    ImageView img_download;

    public ItuneEpisodeViewHolder(View itemView) {
        super(itemView);
        main = itemView.findViewById(R.id.main); //add this
        tvTitle = itemView.findViewById(R.id.tv_podcast_title);
        tvDescription = itemView.findViewById(R.id.tv_description);
        divider = itemView.findViewById(R.id.divider);
        img_download = itemView.findViewById(R.id.img_download);
        main.setOnClickListener(this); //set the onclick on the parent layout
        img_download.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.img_download) {
            mOnClickListener.onDownloadItemClick();
        } else if (view.getId() == R.id.main) {
            mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
        }
    }

XML: XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main" //add this
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true" //add this
android:focusable="true" //add this
android:elevation="4dp">
...
...
  <ImageView
    android:id="@+id/img_download"
    android:clickable="true" //add this
    android:focusable="true" //add this
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_margin="5dp"
    android:tint="@color/primary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_drawer_download" />


</android.support.constraint.ConstraintLayout>

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

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