【问题标题】:SwipeRefreshLayout not called inside ViewPager with tabhost in AndroidSwipeRefreshLayout 未在 Android 中使用 tabhost 的 ViewPager 中调用
【发布时间】:2017-10-03 11:01:38
【问题描述】:

我的活动包含带有 tabhost 的 viewpager,如下所示

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/white"
            app:tabMode="fixed"
            app:tabTextAppearance="@style/MyTextAppearance" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

它包含3个片段,每个片段都包含这样的swiperefreshlayout内的recyclerview,

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_action1" />

        <SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </SwipeRefreshLayout>

</android.support.design.widget.CoordinatorLayout>

但它不会调用 swipeRefreshLayout.setOnRefreshListener.... 有任何问题请帮助我。

【问题讨论】:

标签: android android-recyclerview android-viewpager swiperefreshlayout fragment-tab-host


【解决方案1】:

您在RecyclerView 中缺少android.support.v7.widget,在SwipeRefreshLayout 中缺少android.support.v4.widget

在你的片段中使用它

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.widget.SwipeRefreshLayout>

在您的片段中初始化SwipeRefreshLayout 并调用OnRefreshListener

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
       @Override
       public void onRefresh() {
           //Peform any task
       }
});

【讨论】:

    【解决方案2】:

    始终保持您的布局 xml 像下面提到的代码一样

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/videoPhotoRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:overScrollMode="never" />
    
            </LinearLayout>
        </android.support.v4.widget.SwipeRefreshLayout>
    

    您必须使用一个ViewGroup 布局,如LinearLayout / RelativeLayout / FrameLayout 作为SwipeRefreshLayout 中的父级。

    我希望它的作品。

    如果问题仍未解决,请随时回复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      相关资源
      最近更新 更多