D.O. D.O. - 3 months ago 9
Android Question

My interface class don't work when I call it from a fragment

I'm trying to implement this code in my project but is not working RecyclerView: how to catch the onClick on an ImageView?:

I've implemented a interface to handle onclick imageview and onclick row in a recyclerview. The problem is that when I try to do a click on the imageview or in the row don't happen nothing. If I try the same in my ViewHolder class, works, because I can see the Toast. The problem is that I need that this work from my fragment.

I think that maybe the problem is when I pass listener to my ViewHolder's constructor or when I pass the listener from my fragment.

Some help will be apreciate:

My Adapter class:

private static List<DatabaseModel> dbList;
private static Context context;
private RecyclerViewClickListener listener;


public class MainTimelineRecyclerAdapter extends RecyclerView.Adapter<MainTimelineRecyclerAdapter.AnViewHolder> {

public MainTimelineRecyclerAdapter(Context context, List<DatabaseModel> dbList, RecyclerViewClickListener listener ){
this.dbList = new ArrayList<DatabaseModel>();
this.context = context;
this.dbList = dbList;
this.listener = listener;

}

public static class AnViewHolder extends RecyclerView.ViewHolder {

public TextView name;
public ImageView contact;

public AnViewHolder(final View itemLayoutView, final RecyclerViewClickListener listener) {
super(itemLayoutView);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null)
listener.onRowClicked(getAdapterPosition());
}
});

contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null)
listener.onViewClicked(v, getAdapterPosition());
}
});

}
}

public interface RecyclerViewClickListener {
void onRowClicked(int position);
void onViewClicked(View v, int position);
}
}


My fragment:

public class MainTimeline extends Fragment implements RecyclerViewClickListener {
private RecyclerViewClickListener listener;
.
.
.

mAdapter = new MainTimelineRecyclerAdapter(getActivity(), dbList, listener);


@Override
public void onViewClicked(View view, int position) {
Toast.makeText(getActivity(), "photo", Toast.LENGTH_SHORT).show();

}

@Override
public void onRowClicked(int position) {
Toast.makeText(getActivity(), "view", Toast.LENGTH_SHORT).show();

}
}

Answer

If you want the fragment to listen for the actions happening in the adapter then you need to register it as the listener for the events happening in the adapter(by implementing the interface and passing the fragment as a listener):

public class MainTimeline extends Fragment implements RecyclerViewClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // you don't need a private RecyclerViewClickListener listener; reference as you have the fragment itself
        mAdapter = new MainTimelineRecyclerAdapter(getActivity(), dbList, this); 
    }
    // implement the methods from the interface
}
Comments