开发者

ListView scroll to selected item

开发者 https://www.devze.com 2023-01-12 11:15 出处:网络
I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push u开发者_Go百科p the edit text and the button. I want the list to scroll to the

I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push u开发者_Go百科p the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks


You can use ListView's setSelection(int position) method to scroll to a row.


You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.


For a direct scroll:

getListView().setSelection(11);

For a smooth scroll:

getListView().smoothScrollToPosition(11);

To Scroll to top

getListView().setSelectionAfterHeaderView();

Note

try to call it in post because sometime listview is not yet created while you calling it's method

getListView().postDelayed(new Runnable() {          
    @Override
    public void run() {
        lst.setSelection(15);
    }
},100L);


You should use transcript mode:

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);


Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.


Yo can look For

listView.setSelectionFromTop(position, distanceFromHeader);

It will position the Item at position , specified pixels below the top of listview


You can use

smoothScrollToPosition(position)

Just increase the position of item with 1, and you will get the view of item.

getListView().smoothScrollToPosition(position + 1);


Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.

int duration = 500;  //miliseconds
int offset = 0;      //fromListTop

listview.smoothScrollToPositionFromTop(position,offset,duration);
  • descrease duration to make scrolling faster
0

精彩评论

暂无评论...
验证码 换一张
取 消