OnLongItemClick in RecyclerView
You can add listeners in your custom adapter implementation. It will be something like:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
public interface OnItemClickListener {
public void onItemClicked(int position);
}
public interface OnItemLongClickListener {
public boolean onItemLongClicked(int position);
}
private Fragment mFragment;
public static class ViewHolder extends RecyclerView.ViewHolder {
public View v;
public ViewHolder(View v) {
super(v);
this.v = v;
}
}
public RecyclerViewAdapter(Fragment fragment) {
mFragment = fragment;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFragment.onItemClicked(position);
}
});
holder.v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mFragment.onItemLongClicked(position);
return true;
}
});
}
. . .
}
Note interfaces definition at the beginning. This way you're passing onClick and onLongClick events to your fragment for handling. Pretty much convenient, you know