Android中Adapter中如何用image判断被点击

How to determine with image was click in Adapter in Android

我想弄清楚当图像都在 FrameLayout 中并且视图被 Adapter 膨胀时如何确定用户点击了哪个图像

适配器的 setItemClickListener 如下:(pos 用于确定数组中的哪个元素被单击,因此列表中的第一项为 0,依此类推...):

holder.setItemClickListener(new ItemClickListener() {

    @Override
    public void onItemClick(View v, int pos) {

        switch (pos) {

           case 0:

               // First item was clicked
               break;

           case 1:

               // Second item was clicked
               break;

       }

       Toast.makeText(v.getContext(), "Clicked list item = " + pos, Toast.LENGTH_SHORT).show();

    }

});

以下是包含项目列表的布局:

<FrameLayout 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/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_centerHorizontal="true"
            app:srcCompat="@drawable/background" />


        <ImageView
            android:id="@+id/awayTeam"
            android:layout_width="160dp"
            android:layout_height="150dp"
            android:layout_gravity="top|left" />

        <ImageView
            android:id="@+id/homeTeam"
            android:layout_width="160dp"
            android:layout_height="150dp"
            android:layout_gravity="top|right"
            android:scaleX="-1" />

        </androidx.cardview.widget.CardView>

    </FrameLayout>

</LinearLayout>

我如何才能确定用户在列表中的位置单击了哪个 ImageView?

更新:

class GameViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ItemClickListener mListener;

    private ImageView awayTeamImageView, homeTeamImageView;


    public GameViewHolder(View itemView) {
        super(itemView);

        awayTeamImageView = itemView.findViewById(R.id.awayTeam);

        homeTeamImageView = itemView.findViewById(R.id.homeTeam);

        itemView.setOnClickListener(this);

    }

    Override
    public void onClick(View v) {

        this.mListener.onItemClick(v, getLayoutPosition());

    }

    public void setItemClickListener(ItemClickListener listener) {

        this.mListener = listener;

    }

}

尝试使用以下代码

holder.setItemClickListener(new ItemClickListener() {

@Override
public void onItemClick(View v, int pos) {

    switch (v.getId()) {

       case R.id.awayTeam:

           // click on image awayTeam
           break;

       case R.id.homeTeam:

           // click on image homeTeam
           break;

   }

   Toast.makeText(v.getContext(), "Clicked list item = " + pos, Toast.LENGTH_SHORT).show();

}

});

// Also update your holder code
 public GameViewHolder(@NonNull View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        awayTeamImageView = itemView.findViewById(R.id.awayTeam);
        homeTeamImageView = itemView.findViewById(R.id.homeTeam);
        awayTeamImageView.setOnClickListener(this);
        homeTeamImageView.setOnClickListener(this);
    }