【问题标题】:Android: setOnRefreshListener is called even if I tap on the outside of my ListViewAndroid:即使我点击 ListView 的外部,也会调用 setOnRefreshListener
【发布时间】:2016-07-12 13:41:20
【问题描述】:

我有这个布局基础:

<RelativeLayout
    android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <FrameLayout
            android:id="@+id/list_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"              
            android:background="@color/background" />

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

</RelativeLayout>

在这个片段创建过程中,我添加了一个特定的列表:

 <ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:divider="@color/list_divider"
    android:dividerHeight="1px" />

所以当我点击红色区域(列表视图之外)时,会调用监听器:

mSwipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        ...
    }
});

那么,为什么要调用这个监听器,我可以避免这种行为吗?

【问题讨论】:

  • android:layout_height="wrap_content"更改为android:layout_height="match_parent"或在SwipeRefreshLayout中使用wrap_content作为高度
  • SwipeRefresh 设置为匹配内容,因此红色区域是其中的一部分
  • android:layout_height="wrap_content"
  • 我已经用 wrap_content 更新了我的 XML(见上文)。结果是一样的。
  • 更改相对布局的高度,以换行。

标签: android listview swiperefreshlayout


【解决方案1】:
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_list_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:divider="@color/list_divider"
android:dividerHeight="1px" />

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

<RelativeLayout
    android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/list_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"              
            android:background="@color/background" />

</RelativeLayout>

使用这个 您正在执行滑动以刷新包括 listView 在内的整个片段。

【讨论】:

  • 我看不懂你的帖子?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
相关资源
最近更新 更多