1

I have this SwipeRefreshLayout XML code:

<?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>

Notice that I have this ListView with the ID: lvRelationShip.

In this SwipeRefresh XML file, I want the action of refreshing only to trigger when I scroll to the top of the ListView.

Problem: In the ListView when I scroll down to see other items in the list everything is fine, but when I scroll up to see previous Items, the page just refreshes. I want the list to only refresh when I scroll up at the top of the list.

yatin deokar
  • 730
  • 11
  • 20

3 Answers3

1

Make your ListView a direct child of your RefreshView and also the first child. It believes that it has reached the top probably since there are other elements at the top of your 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>

...

I am writing this on my phone. Excuse the formatting errors. Notice I removed the two layout tags from the top, that is how you will require your layout to be, the list is a direct child and the first element.

Torched90
  • 305
  • 1
  • 3
  • 18
0

Observe scroll position of list and disable SwipeRefreshLayout accordingly, This works for me

 //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);

                }
            });
yatin deokar
  • 730
  • 11
  • 20
0

If RecyclerView or ListView is not the direct child of SwipeRefreshLayout then this issue occurs.

Simplest solution is to provide OnChildScrollUpCallback implementation and return the results appropriately. In Kotlin code below, refreshLayout is SwipeRefreshLayout and recyclerView is RecyclerView as can be seen in xml layout code as well.

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

While xml layout is something like this,

<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>

This is also answered here.

Sabeeh
  • 1,123
  • 9
  • 11