SwipeRefreshLayout inside ViewPager

I have an activity consisting of a toolbar and a presentation pager. Its location is as follows:

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

Then I load two fragments into the presentation pager. Each fragment contains its own SwipeRefreshLayout . I will show one fragment layout:

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

Now when I run the action, it works. However, I noticed that this is horizontal (i.e. go to the next tab). It launches SwipeRefreshLayout OnRefreshListener

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

I also experience odd visual behavior when I try to make an update. The loading sign (twisting circle) is often not displayed, and it sometimes shows, but remains small. I'm not sure if this also causes the two views to conflict with each other.

Is there any way to solve this problem. I am not sure how to do this. Perhaps limiting SwipeRefreshLayout to only listen to vertical scrolls?

Any suggestions would be greatly appreciated.

+5
source share
2 answers

I also had this stupid problem. This seems to be a bug in SwipeRefreshLayout . As a workaround, I had to remove all of its child views in the Fragment.onDestroyView() method.

 public SwipeRefreshLayout mSwipeRefreshLayout; ... @Override public void onDestroyView() { mSwipeRefreshLayout.removeAllViews(); super.onDestroyView(); } 
+3
source

This is because there is an error in SwipeRefreshLayout! Error: onRefresh does not work properly if onMeasure is not called "You must extend SwipeRefreshLayout like this!

 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; } } } 
+1
source

All Articles