How to autoselect 1st item from RecyclerView? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to autoselect 1st item from RecyclerView? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a recyclerView in my app that contain a list of Menu and when i press one of them if will change items in another recyclerView but when i open my activity noone of recyclerView where there are the menu’s are selected.

So the question is how can i open the activity and find the 1st item of the recyclerView selected?

I was trying to simulate an click by Handler but it was making my app slower.

Here is the code from my Adapter :

public class RecyclerViewMenu extends RecyclerView.Adapter<RecyclerViewMenu.MenuViewHolder> {

    private ArrayList<MenuConstructor> menuConstructors;
    private OnItemClickListener onItemClickListener;
    private int selected_position = -1;



    public interface  OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        onItemClickListener = listener;
    }

    @NonNull
    @Override
    public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.menucard,parent,false);
        return new MenuViewHolder(v, onItemClickListener);

    }


    RecyclerViewMenu(ArrayList<MenuConstructor> menuList){
        menuConstructors = menuList;
    }

    @Override
    public void onBindViewHolder(@NonNull final MenuViewHolder holder, final int position) {
        MenuConstructor currentItem = menuConstructors.get(position);

        if (selected_position == position) {
            holder.itemView.setBackgroundColor(Color.parseColor("#2e7d32"));

        } else {
            holder.itemView.setBackgroundColor(Color.parseColor(currentItem.getSfondoColor()));
        }

        holder.textView.setText(currentItem.getDesk());
        holder.textView.setTextColor(Color.parseColor(currentItem.getFontColor()));




    }

    @Override
    public int getItemCount() {
        return menuConstructors.size();
    }

    public class MenuViewHolder extends RecyclerView.ViewHolder {

        public TextView textView;

        MenuViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);

            textView = itemView.findViewById(R.id.ButtonName);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        if(listener != null){
                            int position = getAdapterPosition();
                            if (position != RecyclerView.NO_POSITION){
                                listener.onItemClick(position);
                                selected_position = position;
                                notifyDataSetChanged();
                            }
                        }


                }
            });



        }
    }

}

And here is a screenshot of my activity as you can see “CICCIO” is my first item of the recyclerView but it was selected manually.

enter image description here

Answer

The variable selected_position should be public in a Utils class and then you set selected_position = 0 in onCreate:

public class Utils {
    public static final int selected_position = -1;
}

then you use it in onCreate Utils.selected_position = 0; and in onBindViewHolder if (position == Utils.selected_position)

Of course you have to remove this line: private int selected_position = -1;

We are here to answer your question about How to autoselect 1st item from RecyclerView? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji