【问题标题】:SwipeRefreshLayout with NestedScrollView not scrollingSwipeRefreshLayout 与 NestedScrollView 不滚动
【发布时间】:2018-12-12 19:37:52
【问题描述】:

我想在滚动视图中显示一个 ListView。这似乎在添加 SwipeRefresh 布局之前有效。添加后,刷新工作,但我不能再滚动。

我相信问题出在 XML 代码上。请注意,如果我使用普通的 ScrollView,我可以同时执行这两项操作,但我无法向上滚动,因为 SwipeRefresh 已激活。

这是我的 XML 代码:

<RelativeLayout 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:orientation="vertical"
android:fitsSystemWindows="true"
>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_favorites"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_marginBottom="60dp"
            android:id="@+id/friends_scrollLayout"
            android:layout_below="@id/friends_search">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/favorite_horeca_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:fillViewport="true"
            android:scrollbars="none"
            android:scrollingCache="true">

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

                <ListView
                    android:id="@+id/favorite_horeca_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:dividerHeight="-1dp" />

            </LinearLayout>

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

        </LinearLayout>

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F7F8"
    android:visibility="gone"
    >

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:layout_above="@id/empty_text"
        android:layout_marginBottom="20dp"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/ic_logo_vert_rond"
        />

    <TextView
        android:id="@+id/empty_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No Horecas added to your favorites."
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>

【问题讨论】:

  • 该布局中的两个&lt;LinearLayout&gt;s 和&lt;NestedScrollView&gt; 都是不必要的。只需将&lt;ListView&gt; 直接放在&lt;SwipeRefreshLayout&gt; 中即可。

标签: android xml scrollview swipe swiperefreshlayout


【解决方案1】:

使用 SwipeRefreshLayout 作为父布局,然后使用 NestedScrollView 作为子布局

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </androidx.recyclerview.widget.RecyclerView>
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

【讨论】:

  • 非常感谢。奇迹般有效。它节省了我很多时间
【解决方案2】:
<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:orientation="vertical"
android:fitsSystemWindows="true"
>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_favorites"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/favorite_horeca_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:fillViewport="true"
            android:scrollbars="none"
            android:scrollingCache="true">

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

                <ListView
                    android:id="@+id/favorite_horeca_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:dividerHeight="-1dp" />

            </LinearLayout>

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

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


<RelativeLayout
    android:id="@+id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F7F8"
    android:visibility="gone"
    >

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:layout_above="@id/empty_text"
        android:layout_marginBottom="20dp"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/ic_logo_vert_rond"
        />

    <TextView
        android:id="@+id/empty_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No Horecas added to your favorites."
        android:gravity="center_horizontal|center_vertical"/>

 </RelativeLayout>

</CoordinatorLayout >

这样试试

【讨论】:

    【解决方案3】:

    解决的办法就是把 Scrollview 完全去掉,不用加,SwipeRefreshLayout 已经内置了 Scroll。

    <RelativeLayout 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:orientation="vertical"
    android:fitsSystemWindows="true"
    >
    
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_favorites"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <ListView
                    android:id="@+id/favorite_horeca_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:dividerHeight="-1dp" />
    
    </android.support.v4.widget.SwipeRefreshLayout>
    
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F5F7F8"
        android:visibility="gone"
        >
    
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_centerInParent="true"
            android:layout_above="@id/empty_text"
            android:layout_marginBottom="20dp"
            android:gravity="center_vertical|center_horizontal"
            android:src="@drawable/ic_logo_vert_rond"
            />
    
        <TextView
            android:id="@+id/empty_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="No Horecas added to your favorites."
            android:gravity="center_horizontal|center_vertical"/>
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多