Programmatically scroll to a specific position in an Android ListView

AndroidAndroid Listview

Android Problem Overview


How can I programmatically scroll to a specific position in a ListView?

For example, I have a String[] {A,B,C,D....}, and I need to set the top visible item of the ListView to the index 21 of my String[].

Android Solutions


Solution 1 - Android

For a direct scroll:

getListView().setSelection(21);

For a smooth scroll:

getListView().smoothScrollToPosition(21);

Solution 2 - Android

For a SmoothScroll with Scroll duration:

getListView().smoothScrollToPositionFromTop(position,offset,duration);

>Parameters
>position -> Position to scroll to
>offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
>duration-> Number of milliseconds to use for the scroll

Note: From API 11.

HandlerExploit's answer was what I was looking for, but My listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)


Edit:(From AFDs suggestion)

To position the current selection:

int h1 = mListView.getHeight();
int h2 = listViewRow.getHeight();

mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);  

Solution 3 - Android

Put your code in handler as follows,

public void timerDelayRunForScroll(long time) {
	    Handler handler = new Handler(); 
	    handler.postDelayed(new Runnable() {           
	        public void run() {   
	        	try {
	        		lstView.smoothScrollToPosition(YOUR_POSITION);
	        	} catch (Exception e) {}
	        }
	    }, time); 
	}

and then call this method like,

timerDelayRunForScroll(100);

CHEERS!!!

Solution 4 - Android

The Listview scroll will be positioned to top by default, but want to scroll if not visible then use this:

if (listView1.getFirstVisiblePosition() > position || listView1.getLastVisiblePosition() < position)
            listView1.setSelection(position);

Solution 5 - Android

I have set OnGroupExpandListener and override onGroupExpand() as:

and use setSelectionFromTop() method which Sets the selected item and positions the selection y pixels from the top edge of the ListView. (If in touch mode, the item will not be selected but it will still be positioned appropriately.) (android docs)

    yourlist.setOnGroupExpandListener (new ExpandableListView.OnGroupExpandListener()
    {

    	@Override
    	public void onGroupExpand(int groupPosition) {
                     
    		expList.setSelectionFromTop(groupPosition, 0);
    		//your other code
     	}
    });

Solution 6 - Android

If someone looking for a similar functionality like Gmail app,

The Listview scroll will be positioned to top by default. Thanks for the hint. amalBit. Just subtract it. That's it.

 Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            int h1 = mDrawerList.getHeight();
            int h2 = header.getHeight();
            mDrawerList.smoothScrollToPosition(h2-h1);
        }
    }, 1000);

Solution 7 - Android

If you want to jump directly to the desired position in a listView just use

listView.setSelection(int position);

and if you want to jump smoothly to the desired position in listView just use

listView.smoothScrollToPosition(int position);

Solution 8 - Android

>Handling listView scrolling using UP/ Down using.button

If someone is interested in handling listView one row up/down using button. then.

public View.OnClickListener onChk = new View.OnClickListener() {
             public void onClick(View v) {

                 int index = list.getFirstVisiblePosition();
                 getListView().smoothScrollToPosition(index+1); // For increment. 

}
});

Solution 9 - Android

This is what worked for me. Combination of answers by amalBit & Melbourne Lopes

public void timerDelayRunForScroll(long time) {
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() {           
        public void run() {   
            try {
                  int h1 = mListView.getHeight();
                  int h2 = v.getHeight();

                  mListView.smoothScrollToPositionFromTop(YOUR_POSITION, h1/2 - h2/2, 500);  

            } catch (Exception e) {}
        }
    }, time); 
}

and then call this method like:

timerDelayRunForScroll(400);

Solution 10 - Android

it is easy list-view.set selection(you pos); or you can save your position with SharedPreference and when you start activity it get preferences and setSeletion to that int

Solution 11 - Android

-If you just want the list to scroll up\dawn to a specific position:

myListView.smoothScrollToPosition(i);

-if you want to get the position of a specific item in myListView:

myListView.getItemAtPosition(i);

-also this myListView.getVerticalScrollbarPosition(i);can helps you.

Good Luck :)

Solution 12 - Android

You need two things to precisely define the scroll position of a listView:

To get the current listView Scroll position:

int firstVisiblePosition = listView.getFirstVisiblePosition(); 
int topEdge=listView.getChildAt(0).getTop(); //This gives how much the top view has been scrolled.

To set the listView Scroll position:

listView.setSelectionFromTop(firstVisiblePosition,0);
// Note the '-' sign for scrollTo.. 
listView.scrollTo(0,-topEdge);
 

Solution 13 - Android

I found this solution to allow the scroll up and down using two different buttons.

As suggested by @Nepster I implement the scroll programmatically using the getFirstVisiblePosition() and getLastVisiblePosition() to get the current position.

final ListView lwresult = (ListView) findViewById(R.id.rds_rdi_mat_list);
	.....

		if (list.size() > 0) {
			ImageButton bnt = (ImageButton) findViewById(R.id.down_action);
			bnt.setVisibility(View.VISIBLE);
			bnt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(lwresult.getLastVisiblePosition()<lwresult.getAdapter().getCount()){
						lwresult.smoothScrollToPosition(lwresult.getLastVisiblePosition()+5);
					}else{
						lwresult.smoothScrollToPosition(lwresult.getAdapter().getCount());

					}
				}
			});
			bnt = (ImageButton) findViewById(R.id.up_action);
			bnt.setVisibility(View.VISIBLE);

			bnt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(lwresult.getFirstVisiblePosition()>0){
						lwresult.smoothScrollToPosition(lwresult.getFirstVisiblePosition()-5);
					}else{
						lwresult.smoothScrollToPosition(0);
					}

				}
			});
		}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxime View Question on Stackoverflow
Solution 1 - AndroidHandlerExploitView Answer on Stackoverflow
Solution 2 - AndroidamalBitView Answer on Stackoverflow
Solution 3 - AndroidMelbourne LopesView Answer on Stackoverflow
Solution 4 - AndroidZalak GajjarView Answer on Stackoverflow
Solution 5 - AndroidInet LabsView Answer on Stackoverflow
Solution 6 - AndroidEngineSenseView Answer on Stackoverflow
Solution 7 - AndroidVijayView Answer on Stackoverflow
Solution 8 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 9 - AndroidPrasanna RamaswamyView Answer on Stackoverflow
Solution 10 - AndroidBagherView Answer on Stackoverflow
Solution 11 - AndroidBasil RawaView Answer on Stackoverflow
Solution 12 - AndroidRahul OgaleView Answer on Stackoverflow
Solution 13 - AndroidPancizView Answer on Stackoverflow