How to adjust the swipe down distance in SwipeRefreshLayout?

后端 未结 4 2008
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 16:55

I implemented SwipeRefreshLayout in my app. I need to change the height which has to be scrolled down to call onRefresh() method. Which method should I use to add a custom h

相关标签:
4条回答
  • 2020-12-12 17:12

    This one just did a trick and no more extra code Just wrap your recyclerview to NestedScrollView then this NestedScrollView is wrapped in SwipeRefreshLayout

    Check code below

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
         android:id="@+id/swipe_refresh_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
         <androidx.core.widget.NestedScrollView
               android:layout_width="match_parent"
               android:layout_height="match_parent">
               
               <androidx.recyclerview.widget.RecyclerView
                      android:id="@+id/recycler_view"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent" />
    
         </androidx.core.widget.NestedScrollView>
    
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    
    0 讨论(0)
  • 2020-12-12 17:17

    The reversion 21 of v4 support library has provided a API to set the distance.

    public void setDistanceToTriggerSync (int distance)
    

    Check the document here.

    One more thing, the rev. 21 changed the behaviour of progress bar, it's now a circle image. Have no idea how to get the old way back.

    0 讨论(0)
  • 2020-12-12 17:20

    If you're using API 21 of the support library, refer to and please upvote Han He's answer here. A method exists to set the trigger distance called setDistanceToTriggerSync.


    Prior to API 21, as adneal mentioned, there are no public or internal methods to modify the trigger distance.

    However, if you don't want to keep a copy of the classes to modify the constants, you can use reflection to manually set a trigger distance.

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    
    // define a distance
    Float mDistanceToTriggerSync = yourCalculation(); 
    
    try {
        // Set the internal trigger distance using reflection.
        Field field = SwipeRefreshLayout.class.getDeclaredField("mDistanceToTriggerSync");
        field.setAccessible(true);
        field.setFloat(swipeLayout, mDistanceToTriggerSync);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    If you're using the height of the layout to determine the distance, you may want to use something like a GlobalLayoutListener.

    mDistanceToTriggerSync

    In SwipeRefreshLayout, there is an internal variable called mDistanceToTriggerSync. This is used to determine at what distance to trigger the refresh.

    In the source code, it is set by the code below in SwipeRefreshLayout:

    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    mDistanceToTriggerSync = (int) Math.min(
            ((View) getParent()) .getHeight() * MAX_SWIPE_DISTANCE_FACTOR,
                    REFRESH_TRIGGER_DISTANCE * metrics.density);
    

    The above uses the parent view height and some constants to calculate the trigger distance. MAX_SWIPE_DISTANCE_FACTOR (0.6) and REFRESH_TRIGGER_DISTANCE (120) are private constants in the class that you cannot modify.

    You can use the above to calculate your trigger distance and use your own constants for the swipe distance factors.

    GlobalLayoutListener

    Setting of the mDistanceToTriggerSync can be done inside a global layout listener so that the height of the layout can be retrieved properly for calculating the trigger distance. Calling getHeight on a view in onCreate will always return 0 because it has not been drawn yet. I got the code from here. You may or may not need to do this depending on your requirements.

    ViewTreeObserver vto = swipeLayout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Calculate the trigger distance.
            final DisplayMetrics metrics = getResources().getDisplayMetrics();
            Float mDistanceToTriggerSync = Math.min(
                    ((View) swipeLayout.getParent()).getHeight() * 0.6f,
                    120 * metrics.density);
    
            try {
                // Set the internal trigger distance using reflection.
                Field field = SwipeRefreshLayout.class.getDeclaredField("mDistanceToTriggerSync");
                field.setAccessible(true);
                field.setFloat(swipeLayout, mDistanceToTriggerSync);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            // Only needs to be done once so remove listener.
            ViewTreeObserver obs = swipeLayout.getViewTreeObserver();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                obs.removeOnGlobalLayoutListener(this);
            } else {
                obs.removeGlobalOnLayoutListener(this);
            }
        }
    });
    

    The setting of the variable only needs to be done once if I understand the underlying code properly as it only sets mDistanceToTriggerSync if it is equal to -1. Therefore, it should be safe to do it in the global layout listener once.

    SwipeRefreshLayout

    If you're interested in the source code of SwipeRefreshLayout you can find it here.

    0 讨论(0)
  • 2020-12-12 17:26

    Currently there isn't a public method, or even an internal one for that matter, that can be used to adjust the trigger distance. But you can copy over the three classes from the support library into your project, then modify SwipeRefreshLayout to your liking.

    Here are the classes you'll need to copy over:

    • BakedBezierInterpolator
    • SwipeProgressBar
    • SwipeRefreshLayout

    The tigger distance is calculated using the constants:

     private static final float MAX_SWIPE_DISTANCE_FACTOR = .6f;
     private static final int REFRESH_TRIGGER_DISTANCE = 120;
    

    In your layout change <android.support.v4.widget.SwipeRefreshLayout.../> to <your_path_to_SwipeRefreshLayout.../> and make sure you change your imports, if you need to.

    0 讨论(0)
提交回复
热议问题