Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Refreshing a ListView or GridView using Vertical Swipe Gesture

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.92k
    Comment on it

    We can refresh a ListView or GridView by vertical swipe using SwipeRefreshLayout class. The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. We need to add an OnRefreshListener that notified whenever the swipe to refresh gesture is completed.
    To start the progress animation we need to call setRefreshing(true) and to disable the progress animation we need to call setRefreshing(false).
    In order to add the SwipeRefreshLayout in a ListView:-
    1) Create layout like this:-

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

    2) Now in the activity class, set OnRefreshListener on the instance of SwipeRefreshLayout

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    updateList();
                }
            });
    

    3) You can do any background task in the UpdateList() method, in this example I am just showing the progress animation for 5 second.

     private void updateList() {
    
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // to stop the animation
                                swipeRefreshLayout.setRefreshing(false);
                            }
                        });
                    }
                }).start();
        }
    

    Note:- The progress animation automatically starts when we setOnRefreshListener. If you want to start manually then call setRefreshing(true).
    Similarly we can add the SwipeRefresh Listener on GridView

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: