【问题标题】:How to Use SwipeRefreshLayout with Two Lists如何在两个列表中使用 SwipeRefreshLayout
【发布时间】:2015-12-19 04:56:22
【问题描述】:

在我的 Android 应用程序的一个特定页面中,根据用户选项,将显示网格视图或列表视图。当在应用中激活滑动时,无论显示什么视图,我都想执行一些任意操作。

我尝试了各种配置,这些配置通常会导致一个列表无法正确显示,或者一个列表无法滚动或响应触摸事件。

xml 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list_swipe_refresh_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <GridView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:columnWidth="90dip"
            android:numColumns="auto_fit"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:id="@+id/gridview"/>
        <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:visibility="gone"
            android:id="@+id/listview"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

我使用这个逻辑显示页面

@Override
public void onResume() {
    super.onResume();
    SharedPreferences settings = getActivity().getApplicationContext().getSharedPreferences("settings", 0);
    boolean layout = settings.getBoolean("Layout", false);
    if (layout == true) {
        listview.setVisibility(View.VISIBLE);
        gridview.setVisibility(View.GONE);
    } else {
        listview.setVisibility(View.GONE);
        gridview.setVisibility(View.VISIBLE);
    }

}

我像这样配置 SwipeRefreshLayout:

    swipeRefreshLayout = (SwipeRefreshLayout)list_view.findViewById(R.id.list_swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (!Updater.updateInProgress) {
                Toast.makeText(getActivity().getApplicationContext(), "Update started...", Toast.LENGTH_LONG).show();
                Updater.updateAll();
                swipeRefreshLayout.setRefreshing(false);
            } else {
                Toast.makeText(getActivity().getApplicationContext(), "An update is already in progress.",Toast.LENGTH_LONG).show();
            }
        }
    });

使用此特定配置,如果应用程序被配置为显示列表,则不会出现任何内容,但如果我在 xml 文件中切换 GridView 和 ListView 的顺序,则列表视图将正确显示,但不会显示 GridView。

如何将 SwipeRefreshLayout 附加到多个列表/网格视图?

编辑:为了响应其中一个 cmets,当我尝试将两个 SwipeRefreshLayouts 放入 FrameLayout 时,定义的第一个列表/网格视图将对滑动和点击无响应。这是我尝试过的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_swipe_refresh_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dip"
        android:numColumns="auto_fit"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:id="@+id/gridview"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_swipe_refresh_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:visibility="gone"
        android:id="@+id/listview"/>
</android.support.v4.widget.SwipeRefreshLayout>

在上面的情况下,gridview 对触摸事件完全没有响应。如果我将列表视图换成网格视图,那将是没有响应的列表视图。

【问题讨论】:

    标签: android android-layout user-interface android-listview


    【解决方案1】:

    这是因为 SwipeRefreshLayout 只能容纳一个滚动标签视图。添加两个 SwipeRefreshLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2021-02-14
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多