【问题标题】:Only refresh screen when top of list view (Android Studio)仅在列表视图顶部刷新屏幕(Android Studio)
【发布时间】:2018-02-04 02:12:32
【问题描述】:

我有这个 SwipeRefreshLayout XML 代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:weightSum="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/Theme.AppCompat"
    tools:context="com.tag.instagramdemo.example.MainActivity"
    android:background="@drawable/background1">

    <android.support.design.widget.CoordinatorLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lvRelationShip2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/txtsearch"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Search" />

        <ListView
            android:id="@+id/lvRelationShip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </ListView>

    </LinearLayout>

    <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        android:stretchColumns="0,1,2">

        <TableRow
            android:id="@+id/tableRowFive"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:adjustViewBounds="true"
                android:maxHeight="40dp"
                android:maxWidth="40dp"
                android:scaleType="fitCenter"
                android:src="@drawable/arrow" />

            <TextView
                android:layout_gravity="center_horizontal|bottom"
                android:text="Click To View"
                android:textSize="26dp" />

            <ImageView
                android:adjustViewBounds="true"
                android:maxHeight="40dp"
                android:maxWidth="40dp"
                android:scaleType="fitCenter"
                android:src="@drawable/arrow2" />

        </TableRow>

        <android.support.design.widget.BottomNavigationView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:layout_gravity="bottom"
            android:animateLayoutChanges="true"
            android:background="#ffffff"
            android:clickable="false"
            android:contextClickable="false"
            android:enabled="false"
            app:itemIconTint="@color/text"
            app:itemTextColor="@color/text"
            design:menu="@menu/menu_main" />

    </TableLayout>

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

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

请注意,我有这个 ID 为:lvRelationShip 的 ListView。

在这个 SwipeRefresh XML 文件中,我希望仅在滚动到 ListView 顶部时触发刷新操作。

问题: 在 ListView 中,当我向下滚动查看列表中的其他项目时,一切都很好,但是当我向上滚动查看以前的项目时,页面会刷新。我希望列表仅在我向上滚动列表顶部时刷新。

【问题讨论】:

    标签: android listview android-studio swiperefreshlayout


    【解决方案1】:

    使您的 ListView 成为 RefreshView 的直接子级,同时也是第一个子级。它认为它已经到达顶部可能是因为您的 XML 顶部还有其他元素

    xmlns:design="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:weightSum="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/Theme.AppCompat"
    tools:context="com.tag.instagramdemo.example.MainActivity"
    android:background="@drawable/background1">
    
    <ListView
        android:id="@+id/lvRelationShip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
    </ListView>
    

    ...

    我正在手机上写这个。请原谅格式错误。请注意,我从顶部删除了两个布局标签,这就是您需要布局的方式,列表是直接子元素和第一个元素。

    【讨论】:

    • 我是新人,我该怎么做?
    • 我一头雾水,你改了什么?
    • 编程的工作方式是自上而下。请注意,在 refreshlayout 之后出现的第一件事是您要刷新的 ListView。应该是这样的
    • 是的,您必须重新订购您的视图
    • 无论如何我可以将 EditText 保持在顶部。
    【解决方案2】:

    观察列表的滚动位置并相应地禁用 SwipeRefreshLayout,这对我有用

     //SwipeRefreshLayout refresh-only-when-at-the-top
    
        mSwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
                new ViewTreeObserver.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged() {
                        if (listView.getScrollY() == 0)
                            mSwipeRefreshLayout.setEnabled(true);
                        else
                            mSwipeRefreshLayout.setEnabled(false);
    
                    }
                });
    

    【讨论】:

      【解决方案3】:

      如果RecyclerViewListView 不是SwipeRefreshLayout 的直接子代,则会出现此问题。

      最简单的解决方案是提供OnChildScrollUpCallback 实现并适当地返回结果。在下面的 Kotlin 代码中,refreshLayoutSwipeRefreshLayoutrecyclerViewRecyclerView 可以在 xml 布局代码中看到。

      refreshLayout.setOnChildScrollUpCallback(object : SwipeRefreshLayout.OnChildScrollUpCallback {
        override fun canChildScrollUp(parent: SwipeRefreshLayout, child: View?): Boolean {
          if (recyclerView != null) {
            return recyclerView.canScrollVertically(-1)
          }
          return false
        }
      })
      

      虽然xml 布局是这样的,

      <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
          android:id="@+id/swipeRefresh".../>
          ...
          lots of other views i.e TextView, ImageView, MotionLayout
          ...
          ...
          ...
          <androidx.recyclerview.widget.RecyclerView
             android:id="@+id/recyclerView".../>
             
          ...
          ...
          ...
      </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
      

      这也是here的回答。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2011-05-04
        • 1970-01-01
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多