ViewPager中的SwipeRefreshLayout

时间:2015-05-24 10:28:21

标签: android android-viewpager swiperefreshlayout onscrolllistener

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

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

然后我将两个片段加载到视图寻呼机中。每个片段都包含自己的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>

现在,当我运行活动时,它似乎有效。但是我注意到我是水平滑动(即转到下一个标签)。它会触发swipeRefreshLayout OnRefreshListener

     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.setRefreshing(true);
                DoUpdate();
            }
        });

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

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

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:3)

我也有这个愚蠢的问题。它似乎是SwipeRefreshLayout中的错误。作为一种解决方法,我必须在Fragment.onDestroyView()方法中删除其所有子视图。

public SwipeRefreshLayout mSwipeRefreshLayout;
...

@Override public void onDestroyView() {
    mSwipeRefreshLayout.removeAllViews();
    super.onDestroyView();
}

答案 1 :(得分:1)

这是因为SwipeRefreshLayout中存在错误! 如果没有调用onMeasure,那么错误就是&#34; onRefresh无法正常工作&#34; 你应该像这样扩展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;
        }
    }
}