本文介绍了获取被单击项目在“回收者视图"中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了RecyclerView,并且工作正常.我有一个ArrayList,其中包含回收站视图的数据.每个项目的布局都很复杂.它包含两个frameLayout. framelayout1包含图像和文本,而framelayout2包含图像和四个文本.当用户单击framelayout1时,我想打开Activity1;当用户单击framelayout2时,我想打开Activity2.我已经在Recycler View中搜索了onClick,发现非常有用的.但是,如何获得arrayList的位置以便通过Intent在activity1或activity2中传递它呢?

I have implement a RecyclerView and it works fine. I have an ArrayList which contains the data for the recycler view. The layout of each item is complicated. It contains two frameLayout. The framelayout1 contains an image and a text and the framelayout2 contains an image and four texts. When the user clicks on the framelayout1 I want to open the Activity1 and when the users clicks on the framelayout2 I want to open the Activity2. I have already search for the onClick in Recycler View and I have found very useful this. But how can I get the position of the arrayList in order to pass it via Intent in the activity1 or activity2?

推荐答案

从视图持有者内部尝试getAdapterPosition(),以便获得用户点击的适配器位置.

Try getAdapterPosition() from inside the view holder so that you may get the adapter position of the click the user made.

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

   public ViewHolder(View itemView) {
       super(itemView);
       itemView.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
       Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
       }
 }

有关getAdapterPosition()的更多信息,请遵循以下链接

For more in getAdapterPosition() follow this link

这篇关于获取被单击项目在“回收者视图"中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:45