如何调整SwipeRefreshLayout的向下扫掠距离是多少? [英] How to adjust the swipe down distance in SwipeRefreshLayout?

查看:343
本文介绍了如何调整SwipeRefreshLayout的向下扫掠距离是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序实现SwipeRefreshLayout。我需要更改,必须向下滚动调用onRefresh()方法的高度。哪种方法我应该用它来添加自定义的高度?

SwipeRefreshLayout实施

 私人SwipeRefreshLayout swipeLayout;
 

在OnCreate中

  swipeLayout =(SwipeRefreshLayout)view.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(本);
        swipeLayout.setColorScheme(android.R.color.holo_blue_dark,
                android.R.color.holo_green_dark,
                android.R.color.holo_purple,
                android.R.color.holo_orange_dark);
        swipeLayout.setSoundEffectsEnabled(真正的);
 

实现一个AsyncTask的在onrefresh方法

  @覆盖
公共无效onRefresh()
{
    // AsyncTask的执行某些任务

}
 

解决方案

如果你使用的是支持库的API 21,参考并请upvote汉和的回答的此处。一种方法,存在设置触发距离名为 setDistanceToTriggerSync


此前API 21,如adneal提到的,没有任何公开或内部的方法来修改触发距离。

不过,如果你不希望保留的类的副本修改常量,你可以使用反射可手动设置触发距离。

  swipeLayout =(SwipeRefreshLayout)findViewById(R.id.swipe_container);

//定义距离
浮法mDistanceToTriggerSync = yourCalculation();

尝试 {
    //使用反射设置内部触发距离。
    场场= SwipeRefreshLayout.class.getDeclaredField(mDistanceToTriggerSync);
    field.setAccessible(真正的);
    field.setFloat(swipeLayout,mDistanceToTriggerSync);
}赶上(例外五){
    e.printStackTrace();
}
 

如果您使用的是布局的高度来确定距离,你可能要使用像一个 GlobalLayoutListener

mDistanceToTriggerSync

SwipeRefreshLayout 中,有一个名为 mDistanceToTriggerSync 内部变量。 这被用来确定在什么距离,以触发刷新。

在源$ C ​​$ C,它是由code。在下面设置 SwipeRefreshLayout

 最后DisplayMetrics指标= getResources()getDisplayMetrics()。
mDistanceToTriggerSync =(INT)Math.min(
        ((查看)的getParent()).getHeight()* MAX_SWIPE_DISTANCE_FACTOR,
                REFRESH_TRIGGER_DISTANCE * metrics.density);
 

上面使用父视图高度和一些常量来计算触发的距离。 MAX_SWIPE_DISTANCE_FACTOR (0.6)和 REFRESH_TRIGGER_DISTANCE (120)都在,你不能修改类的私人常量。

您可以用上面计算的触发距离和使用自己的常量,刷卡距离的因素。

GlobalLayoutListener

设置 mDistanceToTriggerSync的可在全球布局监听器里做这样的布局的高度可以检索正确计算触发距离。在的onCreate 一个视图中调用的getHeight 将始终返回0,因为它没有被抽呢。我从的code这里。您可能会或可能不会需要做到这一点取决于您的要求。

  ViewTreeObserver VTO = swipeLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
    @覆盖
    公共无效onGlobalLayout(){
        //计算触发距离。
        最后DisplayMetrics指标= getResources()getDisplayMetrics()。
        浮法mDistanceToTriggerSync = Math.min(
                ((查看)swipeLayout.getParent())。getHeight()都会* 0.6f,
                120 * metrics.density);

        尝试 {
            //使用反射设置内部触发距离。
            场场= SwipeRefreshLayout.class.getDeclaredField(mDistanceToTriggerSync);
            field.setAccessible(真正的);
            field.setFloat(swipeLayout,mDistanceToTriggerSync);
        }赶上(例外五){
            e.printStackTrace();
        }

        //只需要进行一次这样删除监听器。
        ViewTreeObserver观测值= swipeLayout.getViewTreeObserver();
        如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN){
            obs.removeOnGlobalLayoutListener(本);
        } 其他 {
            obs.removeGlobalOnLayoutListener(本);
        }
    }
});
 


变量的设置只需要如果我理解做一次基础code正确,因为它仅设置 mDistanceToTriggerSync 是否等于 1 。因此,它应该是安全的做全球布局听众一次。

SwipeRefreshLayout

如果您有兴趣 SwipeRefreshLayout 来源$ C ​​$ C,你可以找到它<一个href="https://github.com/android/platform_frameworks_support/blob/android-support-lib-19.1.0/v4/java/android/support/v4/widget/SwipeRefreshLayout.java"相对=nofollow>这里。

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 height ?

Implementation of SwipeRefreshLayout

private SwipeRefreshLayout swipeLayout;

In oncreate

swipeLayout   = (SwipeRefreshLayout)view.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_dark, 
                android.R.color.holo_green_dark, 
                android.R.color.holo_purple, 
                android.R.color.holo_orange_dark);   
        swipeLayout.setSoundEffectsEnabled(true);

Implementing an AsyncTask in onrefresh method

   @Override
public void onRefresh() 
{
    // Asynctask to perform some task  

}

解决方案

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.

这篇关于如何调整SwipeRefreshLayout的向下扫掠距离是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆