I have three classes viz. an Fragment, a RecyclerViewAdapter and a RecyclerViewHolder. They work in the usual way. But now I got a problem, I need to call a method in the ViewHolder from the said Activity.
I was thinking of using callbacks but I tried and it's not working. This is what I have done so far:
The interface
public interface TreeBottomListener {
enum Status {FAILED, SUCCESS, PRE_REQUEST}
void onResponse(Status status);
}
The ViewHolder
public class TreeBottomViewHolder extends RecyclerView.ViewHolder implements TreeBottomListener{
@BindView(R.id.tree_bottom_progress) ProgressBar bottomProgress;
@BindView(R.id.tree_error_button) Button moreRetryButton;
private TreeAdapterListener listener;
private final String TAG = "TreeBottomViewHolder";
public TreeBottomViewHolder(View itemView, final TreeAdapterListener listener) {
super(itemView);
ButterKnife.bind(this, itemView);
this.listener = listener;
setUpViews();
}
private void setUpViews() {
bottomProgress.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor
(itemView.getContext(), R.color.colorAccent), PorterDuff.Mode.SRC_IN);
moreRetryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
listener.onLoadMoreClicked();
}
});
}
@Override
public void onResponse(Status status) {
processViews(status);
}
private void processViews(Status status) {
// This method is never called
Log.i(TAG, "onResponse: with status " + status);
if (status == Status.FAILED) {
bottomProgress.setVisibility(View.GONE);
moreRetryButton.setVisibility(View.VISIBLE);
} else if (status == Status.SUCCESS) {
bottomProgress.setVisibility(View.GONE);
} else if (status == Status.PRE_REQUEST) {
bottomProgress.setVisibility(View.VISIBLE);
}
}
}
The Fragment
private TreeBottomListener bottomListener = bottomCallbacks;
private static TreeBottomListener bottomCallbacks = new TreeBottomListener() {
@Override
public void onResponse(Status status) {
}
};
// I do this say when a network request is about to be sent
bottomListener.onResponse(TreeBottomListener.Status.PRE_REQUEST);