Estou seguindo esse tutorial de implementação de TabLayout e ViewPager com RecyclerView:
TabLayout e ViewPager com RecyclerView
Estou usando o referido projeto para testes de como pegar a ação do click no RecyclerView. Para complementar, estou seguindo esse post que trata desse assunto e do mesmo projeto no StackOverFlow:
O que eu fiz:
- Criei a Interface ClickListener;
- Instanciei a interface no adaptador do RecyclerView;
- Usei a função de interface implementando o onClickListener no ViewHolder; e
- Implementei a interface no fragment.
Minha 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);
}
}
FragmentA
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();
}
}
Não consegue achar a referência
Porque está ocorrendo esse erro na implementação para o clique no RecyclerView?