【问题标题】:Android GUI frozen when SwipeRefreshLayout is not used不使用 SwipeRefreshLayout 时 Android GUI 冻结
【发布时间】:2017-03-21 12:35:26
【问题描述】:

我想在我的应用程序中使用滑动刷新功能,但我有一个问题:只需为其添加 XML,它就会破坏 GUI。在应用程序被刷卡的那一刻,我仍然可以使用 GUI,但是当它停止或我回到特定页面时它再次中断。这是我添加了破坏应用程序的 SwipeRefreshLayout 标记的活动 XML:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.something.myapp.UserListActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/user_list" />
</FrameLayout>

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

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

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

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

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

【问题讨论】:

  • 顺便说一下,我可以使用 ScrollView 或 ListView 代替 RecyclerView,如果我在 Java 代码中添加刷新功能,实际的内容刷新将始终有效。它只会弄乱 GUI,这个 XML...

标签: android xml user-interface swiperefreshlayout


【解决方案1】:

好吧,我只花了一整天的时间,但我想通了……我不太了解 XML 逻辑,但过了一会儿我意识到该怎么做。我的解决方案是不要将 SwipeRefreshLayout 放在 CoordinatorLayout 中,而是反过来 - 将 CoordinatorLayout 放在 SwipeRefreshLayout 中,将 SwipeRefreshLayout 放在该 xml 文件层次结构的顶部。

【讨论】:

    【解决方案2】:

    您的问题是 SwipeRefreshLayout 必须是视图的父级,并且应该只有一个子级,您可以在 Android 开发人员文档中看到:

    此布局应作为手势刷新的视图的父级,并且只能支持一个直接子级。

    https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

    当您更改布局时,这条规则不再被打破。

    【讨论】:

    • 是的,文档很清楚,我只需要阅读它并进行实验,不要害怕 xml。
    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多