Finally, Google released an official version of pull-to-refresh library!
It is called
SwipeRefreshLayout
, inside the support library, and the documentation is here:
1) Add
SwipeRefreshLayout
as a parent of view which will be treated as pull to refresh layout. (I took ListView
as an example, it can be any View
like LinearLayout
, ScrollView
etc.)<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
2) Add a listener to your class
protected void onCreate(Bundle savedInstanceState) {
SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshData(); // your code
pullToRefresh.setRefreshing(false);
}
});
}
You can also call
pullToRefresh.setRefreshing(true/false);
as per your requirement.
Comments
Post a Comment