In some of the applications in android like gmail app we can see that if we just swipe down and release the touch the screen will get refresh.It is very easy to get such a functionality in our application by using SwipeRefreshLayout. In this tutorial we will learn how to implement swipe to refresh a listview in android.First step is to add the SwipeRefreshLayout to your layout file like shown below.
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/sw_refresh" android:layout_width="match_parent" android:layout_height="wrap_content"> <!--layout which should be refreshed should come here--> </android.support.v4.widget.SwipeRefreshLayout>
Now you need to have a reference of the layout in your class file and a listener while doing swipe down on the screen to refresh.You need to use the following code.
SwipeRefreshLayout sw_refresh = (SwipeRefreshLayout) findViewById(R.id.sw_refresh); sw_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //code for updating screen //following line is important to stop animation for refreshing sw_refresh.setRefreshing(false); } });
Now let us see a simple example which demonstrates the use of SwipeRefreshLayout. The example will contain a listview which is placed inside the SwipeRefreshLayout , the listview will refresh whenever it swiped down. Let us start by creating new project in android studio.
Step 1:
Create a Project in Android Studio by clicking File->New Project
Step 2:
Fill the required fields like Application name and package name and click Next
Step 3:
Select Basic activity and click Next, and click Finish Button.A default activity will be created as MainActivity.java with layouts activity_main.xml and content_main.xml
After completing the following steps activity_main.xml will be filled with the following code. If not use the below code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.example.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout>
Now add the following code to your content_main.xml which will create a listview inside SwipeRefreshLayout.
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.MainActivity" tools:showIn="@layout/activity_main"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/sw_refresh" android:layout_width="match_parent" android:layout_height="wrap_content"> <!--layout which should be refreshed should come here--> <ListView android:id="@+id/lv_languages" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
Now simply use the below code in your MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity { Toolbar toolbar; ListView lv_languages; SwipeRefreshLayout sw_refresh; ArrayAdapter<String> list_adapter; ArrayList<String> languages; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //mehtod for initailisation init(); //setting listview adapter lv_languages.setAdapter(list_adapter); //action for refreshing should come in the following listener sw_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //here i am adding just one more element to display while refershing // in your case you can request wether now item is there. //or check sqlite for new data if data is from sqlite database //when you get the new data you can add it to arraylist and notify adapter as below languages.add("Android");//new element list_adapter.notifyDataSetChanged(); //following line is important to stop animation for refreshing sw_refresh.setRefreshing(false); } }); } private void init() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Swipe To Refresh"); languages = new ArrayList(); //Adding few items into arraylist languages.add("JAVA"); languages.add("JAVA SCRIPT"); //initialising array adapter for listview list_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, languages); //initialising listview lv_languages = (ListView) findViewById(R.id.lv_languages); //initialising swiperefresh layout sw_refresh = (SwipeRefreshLayout) findViewById(R.id.sw_refresh); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
