about how the ListView scrolls to the middle entry

Source: Internet
Author: User

Because of the project's reason, it is necessary for the ListView to select the item as the middle of the currently visible item, but the method found on the web is too cumbersome, so try it yourself.


Private ListView TESTLV;
private int Center;
private int first;
Testlv.setonitemclicklistener (this);
Testlv.setonscrolllistener (this);

@Override public
void Onitemclick (adapterview<?> adapterview, view view, final int i, long l) {
    if (i > C Enter) {
      //   testlv.setselection (i-center);
        Testlv.post (New Runnable () {
            @Override public
            void Run () {
                if (I-center > First) {
                    Testlv.setselection (I-center);
                } else {
                    testlv.smoothscrolltoposition (i-center);}}}
        );
    } else {
        Testlv.smoothscrolltoposition (0);
    }
}

@Override public
void onscrollstatechanged (Abslistview abslistview, int. i) {

}

@Override
public void Onscroll (Abslistview abslistview, int i, int i1, int i2) {First
    = i;
    Center = I1/2;
}


If you use SetSelection (), then the ListView jumps directly to the specified entry, there is no animation in the middle, and a scrolling animation is used with Smoothscrolltoposition ().

However, when you use the Smoothscrolltoposition () method, if you select below the currently visible entry (for example, the total number of currently visible entries is 10, you select the 8th visible entry), then the ListView does not scroll up, specifically I do not study, Let's make it work, and if anyone has the heart to study the problem, we welcome the discussion.

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.