如何在SwipeRefreshLayout中调整向下滑动距离?

Ani*_*udh 24 android android-support-library swiperefreshlayout

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

SwipeRefreshLayout的实现

private SwipeRefreshLayout swipeLayout;
Run Code Online (Sandbox Code Playgroud)

在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);
Run Code Online (Sandbox Code Playgroud)

在onrefresh方法中实现AsyncTask

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

}
Run Code Online (Sandbox Code Playgroud)

Han*_* He 32

v4支持库的reversion 21提供了一个API来设置距离.

public void setDistanceToTriggerSync (int distance)
Run Code Online (Sandbox Code Playgroud)

这里查看文档.

还有一件事,转.21改变了进度条的行为,它现在是一个圆形图像.不知道如何回归旧路.


sin*_*hum 28

如果您正在使用支持库的API 21,请参阅并请在此处提升韩赫的答案.存在设置被调用的触发距离的方法setDistanceToTriggerSync.


在API 21之前,正如adneal所提到的,没有公共或内部方法来修改触发距离.

但是,如果您不想保留类的副本以修改常量,则可以使用反射手动设置触发距离.

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();
}
Run Code Online (Sandbox Code Playgroud)

如果您使用布局的高度来确定距离,您可能想要使用类似的东西GlobalLayoutListener.

mDistanceToTriggerSync

SwipeRefreshLayout,有一个内部变量叫mDistanceToTriggerSync.这用于确定触发刷新的距离.

在源代码中,它由以下代码设置SwipeRefreshLayout:

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

以上使用父视图高度和一些常量来计算触发距离.MAX_SWIPE_DISTANCE_FACTOR(0.6)和REFRESH_TRIGGER_DISTANCE(120)是类中的私有常量,您无法修改.

您可以使用上述方法计算触发距离,并使用自己的常数作为滑动距离因子.

GlobalLayoutListener

mDistanceToTriggerSync可以在全局布局侦听器内完成设置,以便可以正确检索布局的高度以计算触发距离.调用getHeight视图onCreate将始终返回0,因为它尚未绘制.我从这里得到了代码.根据您的要求,您可能需要也可能不需要这样做.

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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

如果我正确理解了底层代码,那么变量的设置只需要完成一次,因为它只mDistanceToTriggerSync在它等于时才设置-1.因此,在全局布局侦听器中执行一次应该是安全的.

SwipeRefreshLayout

如果您对源代码感兴趣,SwipeRefreshLayout可以在此处找到它.

  • 是的,除了将类复制到此之外,除了向AOSP提交补丁之外,这是唯一的其他解决方法.我们的每个答案都有其缺点,但我认为这是一个很好的答案. (2认同)