Thursday 9 October 2014

HB Blog 28: Pull To Refresh Using SwipeRefreshLayout In Android.

           Observe, when pull down the listview screen, the loading symbol appears and loading content become visible on top the application contents.Then the new contents will be fetched.
We can find many custom library's for this stuff.But, now it's kind of out - dated stuff. We can use SwipeRefreshLayout for pull to refresh functionality in android. You can see this kind of pull to refresh in google applications, etc.
Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,

activity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// It has only View inside the layout. Add the scrollView inside the SwipeRefresh //layout to support pull to refresh. For ListView and GridView no need to add //ScrollView inside SwipeRefreshLayout.

<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" >
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</android.support.v4.widget.SwipeRefreshLayout>

SwipeRefreshActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class SwipeRefreshActivity extends Activity implements OnRefreshListener
{
    SwipeRefreshLayout swipeLayout;
    ListView listView;
    ArrayAdapter adapter;
    ArrayList<String> arrayList;
    String[] array = new String[] { "AAA", "BBB", "CCC", "EEE" };
   
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        //sets Refresh listener for your layout.
        swipeLayout.setOnRefreshListener(this);
        //Loading color schemes are adding
       swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_orange_dark, android.R.color.white, android.R.color.holo_green_dark);
        listView = (ListView) findViewById(R.id.listview);
        adapter = new ArrayAdapter(SwipeRefreshActivity.this, android.R.layout.simple_list_item_1, initialArrayData());
        listView.setAdapter(adapter);
    }

    private ArrayList initialArrayData()
    {
        if(arrayList == null)
            arrayList = new ArrayList();
        for(String items : array){
            arrayList.add(items);
        }
        return arrayList;
    }
    private ArrayList appendData(){
        if(arrayList == null)
        arrayList = new ArrayList();
        arrayList.add("HB");
        return arrayList;
    }

    @Override
    public void onRefresh()
    {
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                appendData();
                adapter.notifyDataSetChanged();
               // to stop the loading progress
                swipeLayout.setRefreshing(false);
               }
           }, 5000);
       }
    }

No comments:

Post a Comment