Ahmed Mujtaba Ahmed Mujtaba - 3 months ago 12
Android Question

How to acquire viewholder items in a callback class

I'm trying to accumulate my recyclerview viewholder's value in another class by passing the viewholder in the callback function. On runtime, I can see the items with their values but how do I extract these values from the view holder? I want to extract the textview value in the callback method. How do I achieve this?

Here's the implementation of my viewholder class

public class CommentViewHolder : RecyclerView.ViewHolder, IItemTouchHelperViewHolder
{
public TextView username;
public ImageView userphoto;
public TextView time;
public TextView comment;
public View _itemView;
public CommentViewHolder(View view)
: base(view)
{
_itemView = view;
}


public void OnItemClear()
{
_itemView.SetBackgroundColor(Color.White);
}

public void OnItemSelected()
{
_itemView.SetBackgroundColor(Color.LightGray);
}
}


Here's the callback method which get's the viewholder.

public override int GetMovementFlags(Android.Support.V7.Widget.RecyclerView recyclerView, Android.Support.V7.Widget.RecyclerView.ViewHolder viewHolder)
{

if (recyclerView.GetLayoutManager() is GridLayoutManager)
{
//do something
}
else
{
//do something
}
}

Answer

It looks like the callback gives you a ViewHolder, so you should be able to get the values through the provided ViewHolder:

if(viewHolder instanceof CommentViewHolder) {
    CommentViewHolder holder = (CommentViewHolder) viewHolder;
    String username = holder.username.getText();
}