Reputation: 61
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??
Upvotes: 6
Views: 15013
Reputation: 8882
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)
}
Upvotes: 0
Reputation: 305
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
Upvotes: 10