Android Recycler - 点击所有地方时调用 OnClickListener

Android Recycler - OnClickListener called when clicking everywhere

我在回收站视图上有一个简单的适配器,在 inflation 上有一个简单的 xml 布局。在 xml 中,我有 2 个文本视图,我只向第一个注册了一个 onClickListener。但是当我点击第二个文本视图时,监听器又被调用了。我点击的任何地方都会调用监听器。

有什么想法吗?谢谢

MyRecyclerViewAdapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private RecyclerView rv;

// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data,RecyclerView rv) {
    this.mInflater = LayoutInflater.from(context);
    this.mData = data;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.test, parent, false);
    return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    String animal = mData.get(position);
    holder.myTextView.setText(animal);
}

// total number of rows
@Override
public int getItemCount() {
    return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView = itemView.findViewById(R.id.tvAnimalName);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Item","clicked");
            }
        });

    }


}

// convenience method for getting data at click position
String getItem(int id) {
    return mData.get(id);
}
}`

测试布局

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
      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:orientation="horizontal"
      android:padding="10dp">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="82dp">

    <TextView
        android:id="@+id/tvAnimalName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Hello World Ankit"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="24dp"
        android:text="byeee"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvAnimalName"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

改变这个:

// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView = itemView.findViewById(R.id.tvAnimalName);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Item","clicked");
            }
        });

    }


}

收件人:

// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView = itemView.findViewById(R.id.tvAnimalName);
         myTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Item","clicked");
            }
        });

    }


}