简体   繁体   中英

How to check which RecyclerView has been clicked

I have a view with two RecyclerViews . They have different ArrayLists, but the same kind of adapters. In the OnClick method in my activity, I want to get which of the Recyclers has been clicked, and then get the item selected.

I tried using view instanceof RecylcerView but that didn't do the trick.

You can add onClickListener to the child layout of 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).

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. If it is RelativeLayout then its parent is second 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.

In your activity's on click listener, you just need to check which View matches the RecyclerView's Row Id.

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. I wrote this answer in Kotlin but you should be able to easily convert to Java :)

I think what you are looking for is just setting onClickListener on each RecyclerView in its Adapter . You can do it by using interfaces, in this case you need to create 2 interfaces:

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:

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. 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. Do not forget to call in onCreate or after you initialised an Adapter to set up those interfaces: 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:

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;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM