Android how to get Android Recyclerview sliding distance

Source: Internet
Author: User

How to get the sliding distance of Recyclerview?

Recyclerview Although there are getscrollx () and getscrolly (), the tests found that both functions always return 0, too silent. So I think of the following methods to achieve the sliding distance:

1. Using Onscrolllistener

        Mrecyclerview.addonscrolllistener (new  Recyclerview.onscrolllistener () {            private int 0 ;            @Override            publicvoidintint  dy) {                -= dy;            }        });

As the code describes, Totaldy does save the Recyclerview sliding distance, but when I swipe down recyclerview and then insert/delete/move item, Totaldy becomes imprecise, such as deleting or inserting a new item, Then Totaldy can no longer return to 0.

Solution: Listen to Recyclerview Addonscrolllistener after their own records onscrolled DY, at the same time to adapter add a Registeradapterdataobserver, monitor insert/delete/move, The DY scroll value that you added to the previous record. I have not tried, do not know whether it is feasible!

2. Some say you can try Computeverticalscrolloffset ()

Totaldy = Recyclerview.computeverticalscrolloffset ();

However, the compute method does not calculate the exact distance of the slide, and the StackOverflow has an answer explaining that the item number is visible on the average height of item, not the exact distance we need.

3. Others say they can try Getchildat (0)

Totaldy = Recyclerview.getchildat (0). GetTop ();

The animation is set by the sliding distance of the first item, but the Totaldy obtained by this method are zeroed after sliding to a certain extent.

This is because Recyclerviewl.getchildat (0) returned is always the first visible child, not the first child of all view lists, so this usage is not a sliding distance.

In addition, the following three usages are equivalent, all to obtain the first visible child:

        This . Getlayoutmanager ();        this. Getchildat (0);        = Layoutmanager.getchildat (0)       int position =  Layoutmanager.findfirstvisibleitemposition ();        = Layoutmanager.getchildat (position)

But the following is not to get the first visible child, but to get the first child of all view lists. But it always returns null after sliding for a distance, that is, after the first child is recycle, it always returns NULL.

this = Layoutmanager.findviewbyposition (0);   

4. If LayoutManager is using Linearlayoutmanager, the following method is strongly recommended to get the sliding distance:

    Public int getscollydistance () {       this. Getlayoutmanager ();        int position = layoutmanager.findfirstvisibleitemposition ();        = layoutmanager.findviewbyposition (position);        int itemheight = firstvisiablechildview.getheight ();        return (position) * ItemHeight- firstvisiablechildview.gettop ();   }

Android how to get Android Recyclerview sliding distance

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.