Android ListView Scroll to Top

Source: Internet
Author: User

1. In order to achieve a similar iOS click on the status bar, the list rolls back to the top of the function (to smooth the scrolling effect), Android click on a button or picture what can also let the ListView scroll to the top (when sliding too many pages, one may not roll to the top, using handler timing monitoring, Continue scrolling until you get to the top)

public static void Scrolltolistviewtop (final Abslistview ListView)
{
Listview.smoothscrolltoposition (0);
Final Handler Handler = new Handler ();
Handler.postdelayed (New Runnable ()
{
@Override
public void Run ()
{
if (listview.getfirstvisibleposition () > 0)
{
Listview.smoothscrolltoposition (0);
Handler.postdelayed (this, 100);
}
}
}, 100);
}

The advantage of this method is that it applies to any ListView (item single style, multiple styles can), and there is no interface display problem

The downside is that when the amount of sliding data is large, the rollback time is longer.

2. For a single-style listview with less data, the scrolling height can be calculated when the slide is stopped, and the Listview.smoothscrollby (Getscrolly (), 500) is used when rolling back;

The ListView Scroll Height principle assumes that the height of each item is certain (it is true in most cases), so you can calculate the total height as soon as you know what item is currently on, because part of the first visible item may have moved out of the boundary, so themListView.getChildAt(0)获得第一个子view,使用getTop()方法计算出这个偏差(得到的一般是负数)。因为是回滚,所以最后滚动距离要是负数,另外可能会有一定的误差,适当的加上个负数,综合起来就可以计算出当前ListView的滚动高度。这种滚动效果比较自然,快到顶部时滚动速度越来越慢,最后停止。

public int getscrolly ()
{
View C = listview.getchildat (0);
if (c = = null)
{
return 0;
}
int firstvisibleposition = Listview.getfirstvisibleposition ();
int top = C.gettop ();

if (firstvisibleposition = = 0)
{
return top;
}
Else
{
Return top-(firstVisiblePosition-1) * C.getheight ()-17;
}
}

3. If only ask to return to the top, do not need scrolling effect, very simple, only need to call Listview.setselection (0) ok.

Android ListView Scroll to Top

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.