Android RecyclerView select first Item
Asked Answered
R

1

8

I'm using a RecyclerView to implement a NavigationDrawer.

I got click events working, but I can't figure out how to have the first item selected on App start and following that keep the selected item higlighted even if the drawer is not shown.

All I've been able to find is multi-selection in RecyclerView.

Revest answered 11/5, 2015 at 12:40 Comment(2)
Add your code of the recyclerview and adapters,,etcc.. to see how can we help you ,)Pirouette
@Smashing already solved my problem, it works :)Diacritic
T
8

I actually just implemented this in an app I am working on. So this method worked:

First create a variable to track the current selected position at the top of your adapter:

private int selectedItem;

Then in your Adapter constructor initiate the selectedItem value you would like:

public NavDrawerMenuListAdapter(Context context, List<NavDrawerItem> data, NavDrawerMenuListViewHolder.NavDrawerMenuClickInterface listener) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.navDrawerItems = data;
        this.listener = listener;
        selectedItem = 0;
    }

Here I use 0 as this is the first item in my menu.

Then in your onBindViewHolder(NavDrawerMenuListViewHolder holder, int position) just check whether your selectedItem == position and set the background of some view to a seleted background like so:

if (selectedItem == position) {
            holder.single_title_textview.setTextColor(0xff86872b);
            holder.nav_drawer_item_holder.setBackgroundColor(Color.DKGRAY);
        } 

Here I set the text color to green and give the Realativelayout parent a gray background on start. You can, of course, customize this in any way you like.

To implement a selection of an item and keep the state I use the following method:

public void selectTaskListItem(int pos) {

        int previousItem = selectedItem;
        selectedItem = pos;

        notifyItemChanged(previousItem);
        notifyItemChanged(pos);

    }

This method I usually call from the OnClick() method.

Hope this helps!.

Turbine answered 11/5, 2015 at 13:53 Comment(7)
this worked perfectly, thank you very much :) sadly i cant upvote your answer yet, im still lacking the reputationDiacritic
This is the wrong way! The view items should have a background item which shows their state.Questionable
Please feel free to provide links to where the "right" way is. All stackoverflow answers had different versions. This one worked for me.Turbine
@Questionable you still have not supplied any link to the correct way?Turbine
see #2038540 @Questionable probably means that you need to use a selector and use state_selected to change an item in the list's stateReinhart
Ahh okay that works. Just wondering whether it will be harder to keep the selected state persistent if you use a selector?Turbine
could you tell me how to select first item from recyclerView, Github repo is github.com/stripe/stripe-terminal-android/blob/master/Example/…Wasteland

© 2022 - 2024 — McMap. All rights reserved.