Recyclerview call Smoothscrolltoposition () to control the sliding speed

Source: Internet
Author: User
Preface

This article is my own study of the summary of this article,
https://mcochin.wordpress.com/2015/05/13/android-customizing-smoothscroller-for-the-recyclerview/"Android: Customizing Smoothscroller for the Recyclerview "
Calling Recyclerview's Smoothscrolltopositon can specify recyclerview to slide to an item, but this slide is fast enough to control if you want the sliding speed to be controlled. Ideas smoothscrolltoposition () method

We know that before calling Recyclerview, you should set a LayoutManager for Recyclerview. Examples are as follows:
Mrecyclerview.setlayoutmanager (Mlinearlayoutmanager);
When calling Mrecyclerview.smoothscrolltoposition, Recyclerview actually calls the code in LayoutManager. From the source code of Recyclerview can be seen:

  public void smoothscrolltoposition (int position) {
        if (Mlayoutfrozen) {
            return;
        }
        if (mlayout = = null) {
            LOG.E (TAG, "cannot smooth scroll without a layoutmanager set." +
                    "call Setlayoutmanager WI Th a non-null argument. ");
            return;
        }
        Mlayout is the LayoutManager mlayout.smoothscrolltoposition that initializes the Recyclerview settings
        (this, mstate, position);
    }
Custom LayoutManager

Next we can customize a layoutmanager, and then the LayoutManager in the Smoothscrolltoposition (this, mstate, position) method to achieve the purpose of controlling the sliding speed. Take Linearlayoutmanager as an example, the code is as follows:

/** * Control sliding speed Linearlayoutmanager */public class Scrollspeedlinearlayoutmanger extends Linearlayoutmanager {private
    float milliseconds_per_inch = 0.03f;

    Private Context Contxt;
        Public Scrollspeedlinearlayoutmanger (Context context) {super (context);
    This.contxt = context;  } @Override public void Smoothscrolltoposition (Recyclerview recyclerview, recyclerview.state State, int position)  {Linearsmoothscroller Linearsmoothscroller = new Linearsmoothscroller (Recyclerview.getcontext ())
                        {@Override public PointF computescrollvectorforposition (int targetposition) { Return scrollspeedlinearlayoutmanger.this. Computescrollvectorforpos
                    Ition (targetposition);
                    }//this returns the milliseconds it takes to//scroll one pixel. @Override Protected float Calculatespeedperpixel (displaymetrics displaymetrics) {return MI
                        lliseconds_per_inch/displaymetrics.density;
        Returns how many milliseconds it takes to slide a pixel};
        Linearsmoothscroller.settargetposition (position);
    Startsmoothscroll (Linearsmoothscroller);
        } public void Setspeedslow () {///own here with density, hope that the same sliding speed on different resolution devices//0.3f is a value of their own reckoned, can be modified according to different needs
    Milliseconds_per_inch = Contxt.getresources (). Getdisplaymetrics (). density * 0.3F; The public void Setspeedfast () {milliseconds_per_inch = Contxt.getresources (). Getdisplaymetrics (). Density * 0.
    03f; }
}

Attached to the Linearlayoutmanager in the Smoothscrolltoposition source code, the comparison can be seen that we just overwrite the Linearsmoothscroller in the Calculatespeedperpixel This method only.

Linearlayoutmanager Source
 @Override public
    void Smoothscrolltoposition (Recyclerview recyclerview, Recyclerview.state State,
            int position) {
        Linearsmoothscroller Linearsmoothscroller =
                New Linearsmoothscroller (Recyclerview.getcontext ()) {
                    @Override public
                    PointF computescrollvectorforposition ( int targetposition) {
                        return linearlayoutmanager.this
                                . computescrollvectorforposition (targetposition);
                    }
                };
        Linearsmoothscroller.settargetposition (position);
        Startsmoothscroll (Linearsmoothscroller);
    

Core approach:

   This returns the milliseconds it takes to
                    //scroll one pixel.
                    @Override
                    protected float calculatespeedperpixel
                    (displaymetrics displaymetrics) {
                        return Milliseconds_ per_inch/displaymetrics.density;
                        Returns how many milliseconds it takes to slide a pixel
                    }

The above is their own summary, we can also go to see the preface posted the original text.

By the way, the pointf mentioned above is described in this class.

/**
 * PointF holds-float coordinates */public
class PointF implements parcelable {public
    float x ;
    public float y;

    Public PointF () {} public

    PointF (float x, float y) {
        this.x = x;
        This.y = y; 
    }

    Public PointF (point P) { 
        this.x = p.x;
        This.y = p.y;
    }

    //... Some set get and parcelable related methods
}

This is a coordinate class, and X, y two points represent the coordinate position. The
is more common in some touch-related examples. Here it is used to indicate the recyclerview direction of the slide.
//for y:use-1 for the direction, 1 for down direction.
//for x (did not the test): Use-1 for Left direction, 1 for right direction.
For example, swipe from ITEM50 to item100, then pointf is (0,-1).

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.