0
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> implements View.OnClickListener {
private List<Categoria> categoriaList;
private Context Context;
public interface ItemClickListener {
void onItemClick(View view, int position);
}
public ListAdapter(List<Categoria> list) {
this.categoriaList = list;
}
@NonNull
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLista = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listagem, parent, false );
Context = itemLista.getContext();
itemLista.setOnClickListener(this);
return new MyViewHolder(itemLista);
}
@Override
public void onClick(View v) {
//getLayoutPosition não esta funcionando
switch (getLayoutPosition()){
case 0:
Context.startActivity(new Intent(Context, Flash3ep_t1.class));
break;
case 1:
Context.startActivity(new Intent(Context, Splash.class));
break;
}
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Categoria categoria = categoriaList.get(position);
holder.titulo.setText(categoria.getTitulo());
holder.line.animate();
}
@Override
public int getItemCount() {
return categoriaList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView titulo;
View line;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.title);
line = itemView.findViewById(R.id.line);
}
}
my only problem is in getAdapterPosition() that is giving error asking to implement a method etc. brother thanks already for the help!. about getAdapterPosition knows what’s going on ?
– WesleyFrost
@Wesleyfrost then you are doing something wrong, because if you are in the class that extends from Recyclerview.Viewholder, it already has the getAdapterPosition method()
– Murillo Comino
@Wesleyfrost actually the error is not only getAdapterPosition, you are only with onClick in this location because in your Adapter you are implementing the View.Onclicklistener, however who is clicked is not the Adapter but the Recycle item, in your case the Myviewholder, onClick should be there and there you will have access to getAdapterPosition() poís it is a Recyclerview.Viewholder method, which is who your Myviewholder extends.
– Alisson Marqui