Recyclerview click item 1 open Activity 1, click item 2 open Activity 2

Asked

Viewed 260 times

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);

        }
    }

1 answer

2


I put your OnClick within the MyViewHolder and use the getAdapterPosition, as in the example below:

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);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (getAdapterPosition()) {
                    case 0:
                        Context.startActivity(new Intent(Context, Flash3ep_t1.class));
                        break;
                    case 1:
                        Context.startActivity(new Intent(Context, Splash.class));
                        break;
                }
            }
        });
    }
}
  • 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 then you are doing something wrong, because if you are in the class that extends from Recyclerview.Viewholder, it already has the getAdapterPosition method()

  • @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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.