How to hook up RecyclerView ViewHolder and container activity / fragment?
I made myself ViewHolder
mine OnClickListener
:
public class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
...
@Override
public void onClick(View view) {
...
}
}
Now I can handle the click event of the list items in the Activity / Fragment. However, I want to give "feedback" to this item. For example, a list item contains TextView
and ProgressBar
, when you click on it, it appears ProgressBar
and TextView
will hide, and the action / fragment will do something like an HTTP request. When the request is complete, ProgressBar
will become invisible until TextView
it reappears. How can I communicate the list item of ViewHolder
this progress?
source to share
The easiest way is to pass the action after creation like this
public class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Activity mActivity;
public ListItemViewHolder (Activity activity) {
mActivity = activity;
}
@Override
public void onClick(View view) {
}
}
source to share
Just use the onClick view to get the link. Example:
@Override
public void onClick(View view) {
someText= (TextView) view.findViewById(R.id.someTextId);
someProgressBar= (ProgressBar) view.findViewById(R.id.someProgressBarId);
someText.setVisibility(View.GONE);
}
If you want to be more specific, create an onClickListener for the element you want
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Someobject obj = objectList.get(position);
holder.vProgressBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do what you want, hide the text or whatever
}
});
...
}
Hope it helps!
source to share