locked
How to implement pull to refresh on ListView? RRS feed

  • Question

  • User218019 posted

    I've seen many tutorials/samples for Xamarin.Forms, but none for Xamarin.Android..

    Wednesday, November 22, 2017 5:29 PM

Answers

  • User35208 posted

    @Krestek You put your ListView inside a "android.support.v4.widget.SwipeRefreshLayout".

    My layout looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:id="@android:id/list" />
        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>
    

    In OnViewCreated() of my fragment I do this and that is all there is to it:

    _swipeRefresh = view.FindViewById<SwipeRefreshLayout>(Resource.Id.swiperefresh);
    if (_swipeRefresh != null)
    {
       var weakTarget = new WeakReference<ContainerListFragment<T>>(this);
        _swipeRefresh.Refresh += (sender, e) =>
        {
            if (weakTarget.TryGetTarget(out ContainerListFragment<T> target)) target.ReloadData(true);
        };
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, November 22, 2017 6:02 PM

All replies

  • User35208 posted

    @Krestek You put your ListView inside a "android.support.v4.widget.SwipeRefreshLayout".

    My layout looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:id="@android:id/list" />
        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>
    

    In OnViewCreated() of my fragment I do this and that is all there is to it:

    _swipeRefresh = view.FindViewById<SwipeRefreshLayout>(Resource.Id.swiperefresh);
    if (_swipeRefresh != null)
    {
       var weakTarget = new WeakReference<ContainerListFragment<T>>(this);
        _swipeRefresh.Refresh += (sender, e) =>
        {
            if (weakTarget.TryGetTarget(out ContainerListFragment<T> target)) target.ReloadData(true);
        };
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, November 22, 2017 6:02 PM
  • User218019 posted

    Thanks, I've understood the axml part, but I didn't understand the weakTarget in the C# code.. I mean, we get the view, and we assign a function to the refresh event, but why is there a _swiperefresh!=null condition and a weak target variable?

    Wednesday, November 22, 2017 6:24 PM
  • User35208 posted

    I use this same code for layouts that might not have a swipe refresh so you can get rid of that check. You should get it working without worrying about the weak stuff. I use it just to make sure I don't have cyclic references. Not sure if it is really needed in this case.

    When I work a lot on Xamarin.iOS, I get into the habit of putting weak stuff in.

    Wednesday, November 22, 2017 6:50 PM
  • User218019 posted

    Ah okay then, it worked. Thank you!

    Wednesday, November 22, 2017 6:58 PM