Jack
Jack

Reputation: 161

How to change spinner selection value by scrolling listview in android

I want to change the value selection of a spinner when a listview is scroll by user. I have no idea how to do this any example would be a great help.

The spinner has list of item such as 1 2 3 4 5 6 7 8 9 10...

And the listview has verse number with a text

  1. text in a listview
  2. text in a listview
  3. text in a listview
  4. text in a listview
  5. text in a listview

So, now when the listview is scroll, I want the spinner item number would change. When the listview has (2. text in a listview) at the top screen then spinner item would change to 2

Upvotes: 1

Views: 974

Answers (1)

TheLittleNaruto
TheLittleNaruto

Reputation: 8473

What I understood from your question is that you want to update your Spinner with the first visible item's position of the ListView while scrolling. So for that all you need to do is to implement OnScrollListener to your ListView:

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                  //Here You can get the first visible item position and can update the spinner respectively.
                  spinner.setSelection(firstVisibleItem);


            }
        }); 

You can see, when you implement OnScrollListener, you overrides two methods onScrollStateChanged and onScroll. And in onScroll method you get the firstVisibleItem's Position and hence you can update the Spinner as well.

Upvotes: 3

Related Questions