How to implement Android Pull-to-Refresh
Finally, Google released an official version of the pull-to-refresh library!
It is called SwipeRefreshLayout
, inside the support library, and the documentation is here:
Add
SwipeRefreshLayout
as a parent of view which will be treated as a pull to refresh the layout. (I tookListView
as an example, it can be anyView
likeLinearLayout
,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>Add a listener to your class
protected void onCreate(Bundle savedInstanceState) {
final 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.
UPDATE
Android support libraries have been deprecated and have been replaced by AndroidX. The link to the new library can be found here.
Also, you need to add the following dependency to your project:
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
OR
You can go to Refactor>>Migrate to AndroidX and Android Studio will handle the dependencies for you.
Pull to refresh recyclerview android
You can use android SwipeRefreshLayout widget instead of ProgressDialog
.
Follow below steps to integrate SwipeRefreshLayout
in your Tab1history
fragment:
1. In your layout tab1history
, add SwipeRefreshLayout
as a root layout and place RecyclewrView
inside it.
// tab1history.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
2. In your Tab1History
fragment, use SwipeRefreshLayout
as below to load data from server:
// Tab1History.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class Tab1History extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView recyclerView;
private CespiteAdapter adapter;
UserSessionManager session;
private static final String URL_DATA = "http://mydata.php";
private List<CespiteOgg> cespiteOggList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tab1history, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
cespiteOggList = new ArrayList<>();
// SwipeRefreshLayout
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
android.R.color.holo_green_dark,
android.R.color.holo_orange_dark,
android.R.color.holo_blue_dark);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
// Fetching data from server
loadRecyclerViewData();
}
});
return rootView;
}
/**
* This method is called when swipe refresh is pulled down
*/
@Override
public void onRefresh() {
// Fetching data from server
loadRecyclerViewData();
}
private void loadRecyclerViewData()
{
// Showing refresh animation before making http call
mSwipeRefreshLayout.setRefreshing(true);
// Session class instance
session = new UserSessionManager(getActivity());
//get user data from session
HashMap<String, String> user = session.getUserDetails();
//get name
String name = user.get(UserSessionManager.KEY_NAME);
// get username
final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME);
StringRequest stringRequest = new StringRequest(Request.Method.POST,
URL_DATA,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("dates");
for(int i=0; i<array.length(); i++)
{
JSONObject o = array.getJSONObject(i);
CespiteOgg item = new CespiteOgg(
o.getString("CodNumInventario"),
o.getString("Nome"),
o.getString("DtCatalogazione"),
o.getString("CodIdA"),
o.getString("username")
);
cespiteOggList.add(item);
}
adapter = new CespiteAdapter(cespiteOggList, getActivity());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Username", usernameUtente);
return params;
}
};
RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest);
}
}
Hope this will work properly.
Pull down to refresh in RecyclerView
You can wrap you RecyclerView with the SwipeRefreshLayout for making a pull to refresh.
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
And you should listen to refresh events for the SwipeRefreshLayout:
refreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Load data to your RecyclerView
refreshData();
}
});
How to create a Pull-to-Refresh into a listView
Perhaps you may want to try to follow this example.
1) Compile this library: compile 'com.android.support:support-v4:21.0.+'
2) Modify your layout like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/activity_main_swipe_refresh_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolBar">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startingList"
android:scrollingCache="false"
android:animationCache="false"
android:smoothScrollbar="true"
android:layout_below="@+id/toolBar" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
3) In your onCreate
method add this code
SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
ListView mListView = (ListView) findViewById(R.id.activity_main_list_view);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
Hope it helps!!!
How to apply pull to refresh feature like chrome in web-view android studio?
if you want to keep the loading till the page loaded you don't have to use mySwipeRefreshLayout.setRefreshing(false)
inside your refresh listener , because your are disabling it just after the refresh start.
instead you have to use it when your loading is done . let say as example you have a webview
, so you should listen on the webview
progress like this example :
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
Pull up to refresh in android RecyclerView
In your Adapter class of RecyclerView/ListView while inflating last item you can put if statement and call a method.
Code if you are using RecyclerView:-
private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if(dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading)
{
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
loading = false;
Log.v("...", "Last Item Wow !");
//Do pagination.. i.e. fetch new data
}
}
}
}
});
Also add below code:-
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
Related Topics
How to Get Onclick Event on Webview in Android
How to Get Resource Name from Resource Id
How to Disable Gradle 'Offline Mode' in Android Studio
Telephonymanager.Getline1Number() Failing
Change Value of R.String Programmatically
What Does Layoutinflater in Android Do
Warning: This Asynctask Class Should Be Static or Leaks Might Occur
Error "The Connection to Adb Is Down, and a Severe Error Has Occurred."
What's the Best Way to Iterate an Android Cursor
Manifest Merger Failed:Uses-Sdk:Minsdkversion 14
Notification Icon with the New Firebase Cloud Messaging System
Android- Error:Execution Failed for Task ':App:Transformclasseswithdexforrelease'
How to Create a Closed (Circular) Listview
Finished with Non Zero Exit Value