简体   繁体   English

如何检查已单击哪个RecyclerView

[英]How to check which RecyclerView has been clicked

I have a view with two RecyclerViews . 我有两个RecyclerViews的视图。 They have different ArrayLists, but the same kind of adapters. 它们具有不同的ArrayList,但具有相同的适配器。 In the OnClick method in my activity, I want to get which of the Recyclers has been clicked, and then get the item selected. 在我的活动中的OnClick方法中,我想获取已单击哪个“ Recyclers ,然后选择该项目。

I tried using view instanceof RecylcerView but that didn't do the trick. 我尝试使用view instanceof RecylcerView但是并没有解决问题。

You can add onClickListener to the child layout of relativeLayout. 您可以将onClickListener添加到relativeLayout的子布局中。

Considering, first RecyclerView has a LinearLayout, and second one has RelativeLayout inside them (What I mean here is that I have considered LinearLayout is the parent layout of individual item in first RecyclerView and RelativeLayout is the parent layout of individual item in second RecyclerView). 考虑到,第一个RecyclerView具有LinearLayout,第二个内部具有RelativeLayout(我的意思是我认为LinearLayout是第一个RecyclerView中单个项目的父布局,而RelativeLayout是第二个RecyclerView中单个项目的父布局)。

linLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        getParentName(v);
    }
});
    relLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        getParentName(v);
    }
});
private void getParentName (View v){
    View view = (View) v.getParent();
    switch (view.getId()) {
        case R.id.linLayout:
            Toast.makeText(MainActivity.this, "First One", Toast.LENGTH_SHORT).show();
            break;
        case R.id.relLayout:
            Toast.makeText(MainActivity.this, "Second One", Toast.LENGTH_SHORT).show();
            break;
    }
}

We know that if it is LinearLayout then its parent is first RecyclerView. 我们知道,如果它是LinearLayout,则其父对象是第一个RecyclerView。 If it is RelativeLayout then its parent is second RecyclerView. 如果是RelativeLayout,则其父级是第二个RecyclerView。

But by using this above mentioned method, we can make root layout of individual child views to have same Layout (LinearLayout or RelativeLayout) and see that the above funtion works. 但是,通过使用上述方法,我们可以使单个子视图的根布局具有相同的布局(LinearLayout或RelativeLayout),并看到上述功能可以正常工作。

In your activity's on click listener, you just need to check which View matches the RecyclerView's Row Id. 在活动的点击侦听器中,您只需要检查哪个视图与RecyclerView的行ID匹配即可。

RecyclerView_row.xml RecyclerView_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView1I"
android:layout_width="match_parent"
android:layout_height="wrap_content">

   <!---  RecyclerView's other Views -->


</RelativeLayout>

MainActivity 主要活动

override fun onItemClick(view: View, position: Int) {

     if(view.id == R.id.recyclerView1I){
         //Do stuff
     }

This is how you determine which RecyclerView was clicked, assuming you have your OnClickListener Interfaces already setup in your adapters. 假设您已经在适配器中设置了OnClickListener接口,这就是确定单击哪个RecyclerView的方式。 I wrote this answer in Kotlin but you should be able to easily convert to Java :) 我在Kotlin中写了这个答案,但是您应该可以轻松地转换为Java :)

I think what you are looking for is just setting onClickListener on each RecyclerView in its Adapter . 我认为您正在寻找的只是在其Adapter每个RecyclerView上设置onClickListener You can do it by using interfaces, in this case you need to create 2 interfaces: 您可以通过使用接口来实现,在这种情况下,您需要创建2个接口:

interface clickListenerInFirstRecycler{
void onFirstClick(int position);
}

interface clickListenerInSecondRecycler{
    void onSecondClick(int position);
}

Depending on which information you want retrieve from an item, just change parameters in interfaces. 根据要从项目中检索的信息,只需更改界面中的参数。 In this case I'll use only to retrieve item's position. 在这种情况下,我将仅用于检索项目的位置。

Then in your Adapter class make global variables of those interfaces and its setter methods: 然后在您的Adapter类中创建这些接口及其setter方法的全局变量:

private  clickListenerInFirstRecycler listener;

public void setOnClickListener(clickListenerInFirstRecycler listener){
    this.listener = listener;
}

Do analogically with second interface and adapter. 与第二个接口和适配器类似。

Then in onBindViewHolder call this variable, in this case listener , method which you created (in first case onFirstClick) and pass appropriate argument. 然后在onBindViewHolder调用此变量,在本例中为您创建的方法listener ,在第一种情况下为onFirstClick,并传递适当的参数。 In this case it's just position: 在这种情况下,它只是位置:

listener.onFirstClick(position);

Then go to your activity and make them implement those interfaces. 然后进行您的活动,并让他们实现这些接口。 You will need to override methods from interfaces and from there you will be able to handle clicks from specified RecyclerView and data from there. 您将需要重写接口中的方法,并且从那里您将能够处理来自指定RecyclerView的单击和来自那里的数据。 Do not forget to call in onCreate or after you initialised an Adapter to set up those interfaces: adapter.setOnClickListener(this) 不要忘记调用onCreate或在初始化Adapter以设置这些接口之后调用: adapter.setOnClickListener(this)

I finally managed to figure this out. 我终于设法弄清楚了。 It was more simple than most of the answers, as it's all solved in the activity onClick method: 它比大多数答案更简单,因为在活动onClick方法中已全部解决:

public void onClick(View v){
    ViewParent parent = v.getParent();
        switch (((RecyclerView) parent).getId()) {
            case R.id.recycler_first:
            // Code
            break;
            case R.id.recycler_second:
            // Code
            break;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何识别单击了哪个“视图”? - How to identify which `view` has been clicked? 检查网格中已单击哪个磁贴的正确方法是什么? - What is the right way to check which tile has been clicked in a grid? 单击了哪个JLabel? - which JLabel has been clicked? 如何使用actionPerformed()检查是否已单击按钮? - How to use actionPerformed() to check to see if a button has been clicked? 如何使JButton知道在哪个面板上单击了它? - How to make JButton know on which panel it has been clicked on? 单击项目后,如何从RecyclerView适配器内部的drawable更改imageView的图像? - How to change image of imageView from drawable inside an adapter of RecyclerView after an item has been clicked? 如何检查我在 JPanel 上绘制的 Rect 是否已被点击? - How would I check if a Rect I have drawn on a JPanel has been clicked? 如何使一个按钮执行一项任务,该任务取决于Java android中单击了哪个先前的按钮? - How to make one button do a task that depends on which previous buttons has been clicked in Java android? 如何识别在可展开列表视图中单击过的groupView? - How to identify a groupView that has been clicked in an expandablelistview? 如何定义已单击哪个ListView? - How to define which ListView have been clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM