Recyclerview单个项目单击侦听器

时间:2017-12-20 11:13:56

标签: android android-recyclerview onclicklistener

  • 我有一个recyclerview适配器,我在其上创建了一个处理click事件的监听器。
  • 我在activity或fragment中实现了这个监听器。 recyclerview项目包含cardview内的大量视图。
  • 我的问题是当我点击 img_download 时没有发生回调(日志不显示)但是当我点击其他空间(不是img_download)时,会发生相应的回调。< / LI>
  • 如何知道点击了哪个视图?

适配器代码:

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

活动实施:

@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");
    }

截图:

enter image description here

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>

1 个答案:

答案 0 :(得分:0)

解决了这个问题,在<select name="city" class="rounded-inputs20 select-select col-md-3"> <option value="Country" selected="selected">Country</option> <option *ngFor="let country of countries.data" value="countries">{{country.name}}</option> </select> <select id="sel1" name="Country" class="rounded-inputs20 select-select col-md-3"> <option value="City" selected="selected">City</option> <option *ngFor="let city of countries.data[0].cities" value="cities"> {{city.name}}</option> </select> (父布局)和ConstraintLayout(ImageView)上添加这两个代码

img_download

而不是android:clickable="true" android:focusable="true" ,在您的itemView上添加ID并在其上设置ConstraintLayout

onClickListener

XML:

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