In my android app I am using an Adapter
inside another Adatper
. Let's say child and parent Adapter
. In parent Adapter
I used onclick
method on an item and instantiate the child Adapter
. Now onClick
on the view in child Adapter
I want to send signal to parent Adapter
and run a function..
This is my code:
// This is in parent layout
holder.liker.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
holder.reaction_layout.setVisibility(View.VISIBLE);
if(holder.cursor.getCount()>0)
{
holder.show_reaction=true;
holder.tinyDB.putString("post_id",String.valueOf(holder.username.getTag()));
holder.tinyDB.putString("act_id",String.valueOf(holder.post_comment.getTag()));
while(holder.cursor.moveToNext())
{
holder.imageModelArrayList.add(new ReactionModel(holder.cursor.getString(1),holder.cursor.getString(3),holder.cursor.getString(2)));
holder.recyclerView.setAdapter(holder.adapter); //Here i instantiate the child adapter
}
}
return true;
}
});
Now this is click function of child Adapter
:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tinyDB.putString("image",imageModelArrayList.get(position).getImage_drawable());
String react_id=String.valueOf(holder.title.getTag());
String act_id=tinyDB.getString("act_id");
String post_id=tinyDB.getString("post_id");
NetworkController.postLikePojoClassCall(base64,Integer.parseInt(act_id),Integer.parseInt(react_id),Integer.parseInt(post_id)).enqueue(new retrofit2.Callback<PostLikePojoClass>() {
@Override
public void onResponse(Call<PostLikePojoClass> call, retrofit2.Response<PostLikePojoClass> response) {
if(response.isSuccessful()) {
System.out.println("response__ " + response.body().getSuccess());
}else{
System.out.println("response__ " + response.errorBody());
}
}
@Override
public void onFailure(Call<PostLikePojoClass> call, Throwable t) {
System.out.println("failure__ " + t.getMessage());
}
});
}
});
In this click I want to send signal to parent Adapter
that item has been clicked.
Use callback method to trigger your parent adapter
Create an interface
public interface MyInterface{
void click();
}
Make parent Adapter implement the interface
class YourParentAdapter extends RecyclerView.Adapter<YourViewHolder> implements MyInterface
Pass your parent adapter into the child adapter through the constructor
public ChildAdapter(MyInterface myInterface){
this.myInterface = interface;
}
Now you can use the function from parent Adapter inside the child Adapter:
myInterface.click();