I have a RecyclerView
that displays twitter like posts. Each list item has a little heart you can press to "Like" the post.
The way it works is that clicking on a heart icon launches an IntentService
which makes the API call. The API then returns a status code (e.g. liked_successfully
).
My issue is that I want to disable that little heart icon while the API call is being made. I can disable it when the user clicks on it by setting that up in the Adapter
but enabling it again is what I do not know how to do.
My IntentService
can broadcast the result to all registered BroadcastReceivers
. I was hoping my Adapter can somehow listen in on that and once more enable the heart icon. But I don't think you can register receivers like that...
Is there some way to solve my problem?
EDIT:
Thanks to the help of ankit aggarwal
's answer I was able to solve my problem. Here is what I did (Note, I chose to go with the BroadcastReceiver
though you could do this the same way with the ResultReceiver
as ankit
suggested):
Step 1: Pass in the adapterPosition
to your service inside the onClick()
method in your adapter:
public ViewHolder(View view) {
super(view);
// Get references to our views
heartImageView = (ImageView) view.findViewById(R.id.heartImageView);
heartImageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Get adapter position
int adapterPosition = getAdapterPosition();
mCursor.moveToPosition(adapterPosition);
// Disable your ImageView
heartImageView.setClickable(false);
// Start your IntentService, passing in the adapterPosition as an extra
//...
});
}
Step 2: Have your IntentService return (either via broadcast or otherwise) that adapterPosition
to your activity where you get a reference to that row and re-enable the ImageView
.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
int adapterPosition = extras.getInt(MyService.ADAPTER_POSITION);
// Re-enable the clicking of the heartImageView for that specific row in RecyclerView
RecyclerView.ViewHolder holder =
mRecyclerView.findViewHolderForAdapterPosition(adapterPosition);
// Get the view from the ViewHolder
View view = holder.itemView;
ImageView heartImageView = (ImageView) view.findViewById(R.id.heartImageView);
// Re-enable the ImageView
heartImageView.setClickable(true);
Log.d("MicroR", "heartImageView enabled again for position: " + String.valueOf(adapterPosition));
}
}