-1
I’m following this implementation tutorial of Tablayout and Viewpager with Recyclerview:
Tablayout and Viewpager with Recyclerview
I’m using this project to test how to catch the click action in Recyclerview. To complement, I’m following this post that deals with this subject and the same project at Stackoverflow:
What I did:
- I created the Clicklistener Interface;
- I installed the interface in the Recyclerview adapter;
- I used the interface function implementing onClickListener on Viewholder; and
- I implemented the interface in Fragment.
My interface
public interface ClickListener {
void itemlistener(int position);
}
Recyclerviewadapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<TextItemViewHolder> {
String[] items;
ClickListener clicklistener;
public RecyclerViewAdapter(String[] items, ClickListener clicklistener) {
this.items = items;
this.clicklistener = clicklistener;
}
@Override
public TextItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_list_item, parent, false);
return new TextItemViewHolder(view);
}
@Override
public void onBindViewHolder(TextItemViewHolder holder, int position) {
holder.bind(items[position]);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return items.length;
}
}
Textitemviewholder
public class TextItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView textView;
public TextItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.list_item);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
clicklistener.itemlistener(getAdapterPosition());
}
public void bind(String text) {
textView.setText(text);
}
}
Fragment
public class FragmentA extends Fragment implements ClickListener{
RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] items = getResources().getStringArray(R.array.tab_A);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(items, this);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
@Override
public void itemlistener(int position) {
Toast.makeText(getContext(), "RecyclerView position is: " + position, Toast.LENGTH_SHORT).show();
}
}
Can’t find the reference
Why is this implementation error occurring to click on Recyclerview?
Related: What is the right approach to get the click/position in Recyclerview?
– ramaral