Reputation: 127
I have an arraylist containing 50 elements, Now I have to show items in listview limited mean I want to show only 10 elements first and when I will use pull to refresh then I want to load more data mean append next 10 items and so on. Can Anyone please tell me How can I do that.
Thanks in advance.
Upvotes: 0
Views: 2630
Reputation: 75788
SwipeRefreshLayout uses a listener mechanism to inform the listener that holds this component that a refresh event occurred, so in other word our Activity, for example, has to implement an interface to be notified.
The Activity is responsible to handle the refresh event and refreshing the corresponding View. If the listener, once it receives the event, determines that the refresh process should take place and wants to show a “refresh animation
”, it has to call setRefrshing(true)
, otherwise it can cancel the animation calling setRefreshing(false)
.
In our case, we are going to use it with ListView. And implement your activity class from SwipeRefreshLayout.OnRefreshListener
. When user swipes down the view, onRefresh() method will be triggered. In you need to take appropriate action in that function like making an http call and fetch the latest data.
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
android.support.v4.widget.SwipeRefreshLayout
can be used to detect user's vertical swipe gesture, to refresh the contents.Hence Clearly Android support library android.support.v4
provides support for in-built Android Pull To Refresh View using SwipeRefreshLayout. SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture.
Please check this android-swipe-down-to-refresh-listview-tutorial And Git DemoAnd Android Pull To Refresh View using SwipeRefreshLayout
Upvotes: 1
Reputation: 3284
android made our day easier by introducing SwipeRefreshLayout in Android.support.v4 to detect the vertical swipe on any view.
Implementing SwipeRefreshLayout is very easy. Whenever you want to detect the swipe down on any view, just the wrap the view around SwipeRefreshLayout element. In our case, we are going to use it with ListView. And implement your activity class from SwipeRefreshLayout.OnRefreshListener
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- place your view here -->
</android.support.v4.widget.SwipeRefreshLayout>
put this code in JAVA file
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
movieList = new ArrayList<>();
adapter = new SwipeListAdapter(this, movieList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchMovies();
}
}
);
}
/**
* This method is called when swipe refresh is pulled down
*/
@Override
public void onRefresh() {
fetchMovies();
}
and for more information Look here
Upvotes: 2