Apply smooth scrolling on List View

蓝咒 提交于 2020-01-04 13:37:23

问题


I am following the answer on this link to span one item of listview at a time on screen. It is working for me but the scrolling and the animation by which item moves up and fits to screen are not working smooth.

I searched and found the method setSelectionFromTop is causing this.

But if i use smoothScrollToPosition then my requirement to fit one item on screen i not fulfilled.

Please help me on this.What shoud I do in order to achieve both

  1. Fit one item of listview at a time on screen
  2. Scrolling should work smooth

回答1:


First, to fit only one item on the screen at a time, you need to set the height of each item to match the height of the ListView.

public View getView(int position, View convertView, ViewGroup parent) {
    ...
    int totalHeight = listView.getHeight();
    int rowHeight = totalHeight / getCount();
    // Set the row height for each of the rows
    ...

With only one item showing at a time, where you use setSelectionFromTop() or smoothScrollToPosition(), it will end up with the same result. So you can use smoothScrollToPosition() now.

Another way to do it is to try setting the selection.

listView.setSelection(position)
listView.setSelectionAfterHeaderView()

setSelectionAfterHeaderView will scroll the ListView to the right position.

Hope this helps.



来源:https://stackoverflow.com/questions/15133654/apply-smooth-scrolling-on-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!