It is my third day now dealing with the handling of my view clicks. I originally was using ListView
, then I switched to RecyclerView
. I have added android:onclick
elements to every control on my row_layout
and I am handling them in my MainActivity
like this:
public void MyMethod(View view) {}
In my old ListView
implementation, I have done setTag(position)
to be able to get it in MyMethod
by doing this inside it:
Integer.parseInt(view.getTag().toString())
This worked nicely without problems. Though now I am dealing with RecyclerView
and being forced to use the ViewHolder
, which does not offer a setTag
method. After searching for 2 hours, I have found that people use setTag
like this:
holder.itemView.setTag(position)
This was acceptable. Though when I try to get the value from the MyMethod
function using the line:
Integer.parseInt(view.getTag().toString())
The application crashes. I have read several implementation of onclick handling inside the adapter which works but I have to use the MainActivity
because I am using something that is unique to that activity.
TL;DR I want to send the position of the clicked row to my MainActivity
in a simple manner.
Edit: I apologize for the confusion since my topic was not very thorough. I have a RecyclerView
and an adapter. The adapter is linked to my row_layout
. This row_layout
xml has one root LinearLayout
. Inside it there is one TextView
, another LinearLayout
(which has two TextViews
) and one Button
(for simplicity). I do not want to suffer for dealing with the clicks on RecylerView
like I did with the ListView
. So, I have decided to add an android:onclick
for every control, then link TextView
and LinearLayout
to a single method and link the Button
(and future Button
s) to their unique methods. What I am missing is that I want to be able to tell the position on each of the receiving methods on my MainActivity
. If I must link everything that comes from the adapter and goes into the MainActivity
to a single onclick handler, so be it. Although, how would I tell which control fired the click?
Edit 2: The requested layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/letter"
android:onClick="MyMethod"
android:layout_width="60dp"
android:layout_height="fill_parent"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="MyMethod"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/firstname"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="17dp" />
<TextView
android:id="@+id/longname"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:text="Test"
android:onClick="OtherMethod"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/process"/>
</LinearLayout>
I think you are stuck with handling multiple clicks on
ReceylerView
if that is the case then let me share you the a code snippet from my project. That's how i handle the click inRecyclerView
you can set click listener to any item in bindViewHolder() like i did for
cardView
I hope this will help some.You can achieve this by creating an interface inside your adapter for an
itemclicklistener
and then you can setonItemClickListener
from yourMainActivity
.Somewhere inside your
RecyclerViewAdapter
you would need the following:Then inside your
ViewHolder
(which I've added as an inner class inside my adapter), you would apply the listener to the components you'd like the user to click, like so:This example shows an
onClickListener
being applied to the image inside aViewHolder
.To implement this code, you would
setOnItemClickListener
to your adapter insideMainActivity
as shown above.EDIT
Because the
View
is getting passed into theOnItemClickListener
, you can perform aswitch
statement inside the listener to ensure that the right logic is being performed to the right component. All you would need to do is take the logic from theMyMethod
function and copy and paste it to the component you wish it to be applied to.Example:
You would also need to change something inside the
ViewHolder
. instead of applying theOnClickListener
to anImageView
, you would need to apply to the whole row like so:EDIT 2
Explanation:
So, with every
RecyclerView
. You need three components, TheRecyclerView
,RecyclerViewAdapter
and theRecyclerViewHolder
. These are what define the actual components the user sees (RecyclerView
) and the Items within that View. The Adapter is where everything is pieced together and the Logic is implemented. The ins and outs of these components are nicely explained by Bill Phillips with the articleRecyclerView
Part 1: Fundamentals ForListView
Experts over at Big Nerd Ranch.But to further explain the logic behind the click events, it's basically utilizing an interface to pass information from the
RecyclerViewAdapter
to theRecyclerViewHolder
to yourMainActivity
. So if you follow the life-cycle of theRecyclerView
adapter, it'll make sense.The adapter is initialized inside your
MainActivity
, the adapter's constructor would then be called with the information being passed. The components would then be passed into the adapter via theOnCreateViewHolder
method. This itself tells the adapter, that's how you would like the list to look like. The components in that layout, would then need to be individually initialized, that's where theViewHolder
comes into play. As you can see like any other components you would initialize in yourActivities
, you do the same in theViewHolder
but because theRecyclerViewAdapter
inflates theViewHolder
you can happily use them within your adapter as shown by Zeeshan Shabbir. But, for this example you would like multiple components to have various logic applied to each individual one in yourMainActivity
class.That's where we create the click listener as a global variable (so it can be accessed by both the
ViewHolder
and theAdapter
) the adapter's job in this case is to ensure the listener exists by creating anInterface
you can initialize the listener through.After you've defined the information you would like the interface to hold (E.G. the component and it's position), you can then create a function in which the adapter will call to apply the logic from your
Activity
(same way you would calledView.OnClickListener
) but by creating asetOnItemClickListener
, you can customize it.This function then needs
onRecyclerViewItemClickListener
variable passed to it, as seen in yourMainActivity
.new RecyclerViewAdapter.onRecyclerViewItemClickListener()
in this case it's the interface you created before with the method inside that would need to be implemented hence theis called.
All the
ViewHolder
does in this scenario is pass the information (The components it's self and the position) into theonItemClickListener
with the components attached (inside theonClick
function) to finalize the actual click functionality.if you would like me to update the explanation in anyway, let me know.