【问题标题】:SwipeRefreshLayout from support lib. v21 doesn't work with static content来自支持库的 SwipeRefreshLayout。 v21 不适用于静态内容
【发布时间】:2014-12-22 10:27:52
【问题描述】:

我正在使用支持库 v21 中的 SwipeRefreshLayout。它适用于 List 或 ScrollView 等可滚动内容,但不适用于静态布局:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="center"
            android:text="Content"/>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

这段代码运行良好。

视频:Example

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"> 

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="center"
            android:text="Content"/>
</android.support.v4.widget.SwipeRefreshLayout>

事实并非如此。

视频:Example

是否可以在 SwipeRefreshLayout 中处理不可滚动的内容?

【问题讨论】:

  • 是的,这是可能的。你到底遇到了什么麻烦?
  • 加载器出现片刻后立即消失
  • 而且这个进度条(加载器)并没有从顶部移动,只是出现一瞬间然后消失

标签: android android-support-library swiperefreshlayout


【解决方案1】:

更新:

此问题现已在支持库的 24.2.0 版中得到修复。


原答案:

这是支持库版本 21 中的回归,因为阻力计算是来自 SwipeRefreshLayoutonTouchEvent() 回调中的 removed,并且仅保留在 onInterceptTouchEvent() 回调中。因此SwipeRefreshLayout 只有在它拦截来自(触摸消费)子View 的触摸事件时才能正常工作。有趣的是,当 SwipeRefreshLayout 最初在支持库的版本 19.1.0 中引入时也存在此问题,但在版本 20 中是 fixed

我已在https://code.google.com/p/android/issues/detail?id=87789 的问题跟踪器上报告此问题

这可以通过扩展 SwipeRefreshLayout 并将其 onTouchEvent() 回调重定向到 onInterceptTouchEvent() 直到它返回 true 来修复:

public class FixedSwipeRefreshLayout extends SwipeRefreshLayout {
    public FixedSwipeRefreshLayout(Context context) {
        super(context);
    }

    public FixedSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private boolean handleTouch = true;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = MotionEventCompat.getActionMasked(ev);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                handleTouch = false;
                break;
            default:
                if (handleTouch) {
                    return super.onTouchEvent(ev);
                }
                handleTouch = onInterceptTouchEvent(ev);
                switch (action) {
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        handleTouch = true;
                        break;
                }
                break;
        }
        return true;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2020-05-07
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多