Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView : get position and switch to activity

How is it possible to get the position of the item which was clicked in the RecyclerView row and than make a intent to an activity in the onClick method ? Here is my adapter:

public class SergejAdapter extends RecyclerView.Adapter<SergejAdapter.MyViewHolder>{

    private LayoutInflater inflater;
    List<Information> data= Collections.emptyList();

    public SergejAdapter(Context context, List<Information> data){
      inflater = LayoutInflater.from(context);
        this.data=data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.navigation_drawer_custom_row,parent,false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView){
            super(itemView);
            title= (TextView) itemView.findViewById(R.id.listText);
            icon = (ImageView) itemView.findViewById(R.id.listIcon);
            icon.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Context context = itemView.getContext();
            Intent intent = new Intent(context, Ernaehrung.class);
            context.startActivity(intent);

        }
    }

Here I just have an onClick when one of the images was clicked, but every image has the same intent.

like image 222
SerOsDeveloper Avatar asked Jan 11 '15 11:01

SerOsDeveloper


Video Answer


3 Answers

You can get postion from ViewHolder by using getAdapterPosition() method. Like in code below:

public class SergejAdapter extends RecyclerView.Adapter<SergejAdapter.MyViewHolder>{

    ...

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{


        @Override
        public void onClick(View v) {
            // here you use position
            int position = getAdapterPosition();
            ...

        }
    }
}
like image 120
Konrad Krakowiak Avatar answered Oct 27 '22 18:10

Konrad Krakowiak


or you can just add :

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder > {

...
    class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder (View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Element " + getAdapterPosition() + " clicked.");
                }
            });
        }


    }
like image 27
Thiago Avatar answered Oct 27 '22 17:10

Thiago


Within your onClick(View v) you can simply call getLayoutPosition() which will return the position where the click happened. Check the official docs for further information.

So your code would be something like this:

@Override
public void onClick(View v) {
    switch(getLayoutPosition()){
       //TODO
    }
}

Preferably you should use an Interface to handle the click.

like image 24
reVerse Avatar answered Oct 27 '22 19:10

reVerse