Android ListView 滚动到所选项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:29:55  来源:igfitidea点击:

ListView scroll to selected item

android

提问by Anita

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 up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks

我有一个带有编辑文本的 ListView 和它下面的按钮。当我点击一个 listView 项目时,键盘出现并向上推编辑文本和按钮。我希望列表滚动到所选项目。任何的想法?谢谢

回答by androidworkz

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

您可以使用 ListView 的setSelection(int position)方法滚动到一行。

回答by Rajesh

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

您可以使用 ListViewsmoothScrollToPosition(int position)滚动到列表中的特定位置。

回答by Jawad Zeb

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

尝试在 post 中调用它,因为有时在调用它的方法时还没有创建 listview

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

回答by Jin35

You should use transcript mode:

你应该使用transcript mode

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

回答by Doge

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.

在被单击的列表项上设置一个侦听器,然后使用View.getTop()View.getBottom()在单击时获取它在父项中的位置。然后您可以使用ListView.scrollTo(x, y)滚动到列表项。

回答by Pavan Jaju

Yo can look For

你可以寻找

listView.setSelectionFromTop(position, distanceFromHeader);

listView.setSelectionFromTop(position, distanceFromHeader);

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

它会将 Item 定位在 listview 顶部下方指定像素的位置

回答by Prashant Kumar Sharma

You can use

您可以使用

smoothScrollToPosition(position)

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

只需将 item 的位置增加 1,您将获得 item 的视图。

getListView().smoothScrollToPosition(position + 1);

回答by Adam

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
  • 减少持续时间以加快滚动速度