How to enable pull to refresh in a scrollview?
Asked Answered
N

2

6

Here is my layout xml. I would like to be able to pull the scroll view to refresh.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/garae_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:fillViewport="true"
android:focusableInTouchMode="false" >
   <RelativeLayout>
      ...
      <com.handmark.pulltorefresh.library.PullToRefreshScrollView>
         <LinearLayout/>
      </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
   </RelativeLayout>
</ScrollView>

I already tried the solution in this link: ScrollView Inside ScrollView

However, it didn't work. How can I use child PullToRefreshScrollView??

Ness answered 10/4, 2014 at 1:6 Comment(0)
V
10

If I understand your question, you want to implement a Pull to refresh in your Scrollview. Instead, to use the library you are using, I would suggest you to implement a SwipeRefreshLayout https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html It is a layout that implements a pull-to-refresh like in the application of Google+ or Gmail.

Here's an example implementing SwipeRefreshLayout in your xml:

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

    <FrameLayout
        android:layout_width="match_parent" android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" >

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipe_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

          <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/garae_scroll"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

         </ScrollView>

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

NOTE

It is highly not recommended to put a Scrollview/Listview inside a Scrollview/Listview. The first reason is about the performance and Romain Guy explains that in one video https://www.youtube.com/watch?v=wDBM6wVEO70

Vary answered 7/8, 2014 at 20:18 Comment(2)
Thanks Hugo I checked so much example but none mentioned that we need to implement on query listener.Valentine
developer.android.com/training/swipe/add-swipe-interfaceRecrudesce
H
0

You can use androidx SwipeRefreshLayout. To do so, you first need to import it in gradle:

dependencies {
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
}

Next, in your XML, wrap the the view that you want to be able to swipe to refresh (in your case the ScrollView) with a androidx.swiperefreshlayout.widget.SwipeRefreshLayout:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ...>
        ...
    </ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Then to set up a refresh listener in your code, use setOnRefreshListener. When the refresh is finished, call setRefreshing(false), otherwise the refresh circle will stay there for ever (in the example below the refresh is synchronous in which case it just goes at the end of the listener). For example in Kotlin:

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

val swipeRefreshLayout = this.findViewById<SwipeRefreshLayout>(R.id.swipeRefreshLayout)
swipeRefreshLayout.setOnRefreshListener {
    // Insert your code to refresh here

    swipeRefreshLayout.setRefreshing(false)
}
Howlyn answered 24/8 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.