ViewPager中的SwipeRefreshLayout

Zap*_*ica 6 android android-viewpager onscrolllistener swiperefreshlayout

我有一个由工具栏和视图寻呼机组成的活动.其布局如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="112dp"
    android:layout_gravity="bottom"
    tools:context=".Rhino68PanelActivity" />

<LinearLayout
    android:id="@+id/toolbarContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <include
        android:id="@+id/tap_strip"
        layout="@layout/tab_strip" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我将两个片段加载到视图寻呼机中.每个片段包含在它自己的SwipeRefreshLayout.我将展示一个片段布局:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="12"
    tools:context=".Rhino68PanelActivity$DummySectionFragment">
    ...        
  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView_panel_status"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在,当我运行活动时,它似乎工作.但是我注意到我是水平滑动(即转到下一个标签).它会触发swipeRefreshLayoutOnRefreshListener

     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.setRefreshing(true);
                DoUpdate();
            }
        });
Run Code Online (Sandbox Code Playgroud)

当我尝试刷新时,我也会遇到奇怪的视觉行为.加载标志(旋转圆圈)通常不会显示,有时会显示但仍然很小.我不确定这是否也是导致两种观点相互冲突的原因.

有什么方法可以解决这个问题.我不确定如何.也许通过将SwipeRefreshLayout限制为仅侦听垂直滑动?

任何建议将不胜感激.

The*_*ist 1

是因为SwipeRefreshLayout有bug!bug 是“如果不调用 onMeasure,onRefresh 将无法正常工作”。您应该像这样扩展 SwipeRefreshLayout!

public class SwipeRefreshLoading extends SwipeRefreshLayout {
    public SwipeRefreshLoading(Context context) {
        super(context, null);
    }

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

    private boolean mMeasured = false;
    private boolean mPreMeasureRefreshing = false;

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!mMeasured) {
            mMeasured = true;
            setRefreshing(mPreMeasureRefreshing);
        }
    }


    @Override
    public void setRefreshing(boolean refreshing) {
        if (mMeasured) {
            super.setRefreshing(refreshing);
        } else {
            mPreMeasureRefreshing = refreshing;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)