获取在 RecyclerView 中单击的特定项目的 ID [重复]

Posted

技术标签:

【中文标题】获取在 RecyclerView 中单击的特定项目的 ID [重复]【英文标题】:Get id of specific item clicked in RecyclerView [duplicate] 【发布时间】:2018-10-24 03:59:54 【问题描述】:

我在 OnItemClickListener 接口的 OnItemClicked(int position, View v) 方法中传递位置和视图。现在在我的 ViewReceptionistActivity 中,我想检查单击了哪个项目和哪个子部分。如果用户单击图像,则执行任务 A 否则,如果用户单击名称,则执行任务 B 等等..

这是我的布局文件 - viewreceptionist_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    cardElevation="5dp"
    contentPadding="10dp"
    app:cardCornerRadius="20dp"
    app:cardUseCompatPadding="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="horizontal"
    android:weightSum="12"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="@drawable/card_background">

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_marginLeft="5dp"
        android:layout_weight="2">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/viewReceptionistImage"
            android:layout_
            android:layout_
            app:civ_border_
            android:layout_gravity="center"
            app:civ_border_color="@color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:layout_weight="9">

        <TextView
            android:id="@+id/textViewViewReceptionistName"
            android:layout_
            android:layout_
            android:text="Name"
            android:textSize="18dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textViewViewReceptionistEmail"
            android:layout_
            android:layout_
            android:text="Email"
            android:textSize="15dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textViewViewReceptionistMobile"
            android:layout_
            android:layout_
            android:text="Mobile No."
            android:textSize="15dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    </LinearLayout>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_marginLeft="5dp"
        android:layout_weight="1">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/viewReceptionistGender"
            android:layout_
            android:layout_
            app:civ_border_
            android:layout_gravity="center"
            app:civ_border_color="@color/black" />

    </LinearLayout>

</LinearLayout>
</android.support.v7.widget.CardView>

这是我的适配器 - ViewReceptionistAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class ViewReceptionistAdapter extends RecyclerView.Adapter<ViewReceptionistAdapter.ViewReceptionistHolder>

    Context context;
    int resource;
    ArrayList<Receptionist> objects;

    OnItemClickListener listener;

    public interface OnItemClickListener
    
        void OnItemClicked(int position, View v);
    

    public void setOnItemClickListener(OnItemClickListener listener)
    
        this.listener = listener;
    

    public ViewReceptionistAdapter(Context context, int resource, ArrayList<Receptionist> objects)
    
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    

    @Override
    public ViewReceptionistHolder onCreateViewHolder(ViewGroup parent, int viewType)
    
        View view = LayoutInflater.from(context).inflate(resource, parent, false);

        ViewReceptionistHolder viewReceptionistHolder = new ViewReceptionistHolder(view);

        return viewReceptionistHolder;
    

    @Override
    public void onBindViewHolder(ViewReceptionistHolder holder, int position)
    
        Receptionist receptionist = objects.get(position);

        holder.tvViewReceptionistName.setText(receptionist.name);
        holder.tvViewReceptionistEmail.setText(receptionist.email);
        holder.tvViewReceptionistMobile.setText(receptionist.mobile);

        String url = receptionist.imageUrl;
        Picasso.get().load(url).into(holder.viewReceptionistImage);


        String gender;

        gender = receptionist.gender;

        if(gender.equals("Male"))
        
            holder.viewReceptionistgender.setImageResource(R.drawable.male_icon);
        
        else
        
            holder.viewReceptionistgender.setImageResource(R.drawable.female_icon);
        
    

    @Override
    public int getItemCount()
    
        return objects.size();
    

    public class ViewReceptionistHolder extends RecyclerView.ViewHolder
    

        public TextView tvViewReceptionistName;
        public TextView tvViewReceptionistEmail;
        public TextView tvViewReceptionistMobile;
        public CircleImageView viewReceptionistImage;
        public CircleImageView viewReceptionistgender;

        public ViewReceptionistHolder(final View itemView)
        
            super(itemView);

            tvViewReceptionistName = itemView.findViewById(R.id.textViewViewReceptionistName);
            tvViewReceptionistEmail = itemView.findViewById(R.id.textViewViewReceptionistEmail);
            tvViewReceptionistMobile = itemView.findViewById(R.id.textViewViewReceptionistMobile);

            viewReceptionistImage = itemView.findViewById(R.id.viewReceptionistImage);
            viewReceptionistgender = itemView.findViewById(R.id.viewReceptionistGender);

            itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v)
                
                    if(listener != null)
                    
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        
                            listener.OnItemClicked(position, v);
                        
                    

                
            );
        
    

这里是 ViewReceptionistActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;

public class ViewReceptionistActivity extends AppCompatActivity

    RecyclerView recyclerView;
    ArrayList<Receptionist> receptionistsList;

    ViewReceptionistAdapter viewReceptionistAdapter;

    FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_receptionist);

        getSupportActionBar().setTitle("View Receptionist");

        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        firestore = FirebaseFirestore.getInstance();
        receptionistsList = new ArrayList<Receptionist>();


        Query query = firestore.collection("Receptionist").orderBy("name", Query.Direction.ASCENDING);

        query.addSnapshotListener(this, new EventListener<QuerySnapshot>() 
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e)
            
                for(DocumentSnapshot documentSnapshot: documentSnapshots)
                
                    Receptionist receptionist = documentSnapshot.toObject(Receptionist.class);
                    receptionistsList.add(receptionist);

                    viewReceptionistAdapter.notifyDataSetChanged();
                
            
        );

        viewReceptionistAdapter = new ViewReceptionistAdapter(this, R.layout.viewreceptionist_item, receptionistsList);

        viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() 
            @Override
            public void OnItemClicked(int position, View v)
            

                Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " " + v, Toast.LENGTH_SHORT).show();

//HERE I WANT TO CHECK IF USER CLICKS AN IMAGE OR NAME OR EMAIL...
//IF USER CLICKS IMAGE - PERFORM TASK A
//IF USSER CLICKS NAME - PERFORM TASK B...AND SO ON


            
        );

        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);


        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(viewReceptionistAdapter);

    

我想从视图中获取点击的特定项目...

【问题讨论】:

@Raj 你成功了吗? 是的,该方法工作正常。但是如果我们在回收站视图项中有 5-6 个视图,它就太长了。但是我正在尝试是否将视图与方法中的位置一起传递那么我们必须有一种方法从该视图中获取一个项目,以便我们可以处理它而不是一次又一次地设置点击侦听器。我也尝试通过从持有者传递 itemView 但没有发生任何事情.. 如果不使用点击监听器,就无法检测点击。您将无法仅通过检测父视图中的点击来检查子视图是否被点击。 好的,谢谢,我只会用这种方法...... 【参考方案1】:

您最好使用方法扩展您的点击界面,以便为每个视图添加对点击事件的反应:

public interface OnItemClickListener
    
        void OnItemImageClicked(int position, View v);
        void OnItemNameClicked(int position, View v);
    

在你的viewHolder中:

viewReceptionistImage.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v)
                
                    if(listener != null)
                    
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        
                            listener.OnItemImageClicked(position, v);
                        
                    

                
            );


tvViewReceptionistName.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v)
                
                    if(listener != null)
                    
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        
                            listener.OnItemNameClicked(position, v);
                        
                    

                
            );

然后在您的 ViewReceptionistActivity 中,您只需实现接口并覆盖方法(因为它们会很多)。 然后像这样在适配器中设置监听器:

viewReceptionistAdapter.setOnItemClickListener(this);

或者你可以使用旧方法:

viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() 
            @Override
            public void OnItemImageClicked(int position, View v)
            
             Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + "Image View: " + v, Toast.LENGTH_SHORT).show();

            

            @Override
            public void OnItemNameClicked(int position, View v)
            
              Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " TextView: " + v, Toast.LENGTH_SHORT).show();

            
        );

【讨论】:

以上是关于获取在 RecyclerView 中单击的特定项目的 ID [重复]的主要内容,如果未能解决你的问题,请参考以下文章

从片段中获取意图值后,我如何在 recyclerview 项目中实现单击

Recyclerview 根据单击的项目获取项目值

RecyclerView - 获取特定位置的视图

如何更新/刷新 RecyclerView 中的特定项目

如何从recyclerview适配器获取选定项目的数量?

过滤 RecyclerView 项目并单击它后会给出错误的项目