Android practice simple tutorial-19th gun (SwipeRefreshLayout pull-down refresh instance), swiperefreshlayout

Source: Internet
Author: User

Android practice simple tutorial-19th gun (SwipeRefreshLayout pull-down refresh instance), swiperefreshlayout

Let's take a look at the specific usage of SwipeRefreshLayout. As the name suggests, this component is a layout, but note that onlyA direct sub-View. In fact, we can see through the documentation that SwipeRefreshLayout only inherits the ViewGroup.

We can see that there is an interface in SwipRefreshLayout, through which we can monitor the sliding gesture. In fact, the most important step to use this component is to implement the onRefresh method of this interface, update Data in this method. As follows:


Method in the interface:


In addition to the OnRefreshListener interface, SwipRefreshLayout has some other important methods, such:

 

1. setOnRefreshListener (SwipeRefreshLayout. OnRefreshListener listener): sets the gesture slide listener.

 

2. setProgressBackgroundColor (int colorRes): sets the background color of the Progress circle.

 

3. setColorSchemeResources (int... ColorResIds): sets the color of the Progress animation.

 

4. setRefreshing (Boolean refreshing): Set the refresh status of the component.

 

5. setSize (int size): Set the progress circle size. There are only two values: DEFAULT and LARGE.

Next, let's take an example to see how to use it.

1. main. xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_refresh"        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></LinearLayout>
2. list_item.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/item_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="15dp"        android:gravity="center"        android:singleLine="true"        android:textSize="16sp"        android:textStyle="bold" /></RelativeLayout>

3. ItemInfo. java:

Package com. demo. downrefresh;/*** the item attribute in ListView ** @ author w. w */public class ItemInfo {/*** id */private int id;/*** name */private String name; public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}}

4. ListViewAdapter. java:

Package com. demo. downrefresh; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. textView;/*** ListView adapter * @ author w. w */public class ListViewAdapter extends ArrayAdapter <ItemInfo> {private LayoutInflater inflater; public ListViewAdapter (Context context, List <ItemInfo> list) {super (context, 0, list ); inflater = LayoutInflater. from (context) ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ItemInfo info = getItem (position); if (convertView = null) {convertView = inflater. inflate (R. layout. item_listview, null);} TextView name = (TextView) convertView. findViewById (R. id. item_name); name. setText (info. getName (); return convertView ;}}
5. MainActivity. java:

Package com. demo. downrefresh; import java. util. arrayList; import java. util. list; import android. r. integer; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. support. v4.widget. swipeRefreshLayout; import android. widget. listView;/*** home page * @ author w. w */public class MainActivity extends Activity implements SwipeRefreshLayout. onRefreshListener {/*** Add a drop-down refresh to the ListView */private SwipeRefreshLayout swipeLayout;/*** ListView */private ListView listView;/*** ListView adapter */private ListViewAdapter adapter; private List <ItemInfo> infoList; private int I = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); swipeLayout = (SwipeRefreshLayout) this. findViewById (R. id. swipe_refresh); swipeLayout. setOnRefreshListener (this); // The swipeLayout style refreshed on the top. setColorScheme (android. r. color. holo_red_light, android. r. color. holo_green_light, android. r. color. holo_blue_bright, android. r. color. holo_orange_light); infoList = new ArrayList <ItemInfo> (); ItemInfo info = new ItemInfo (); info. setName ("coin"); infoList. add (info); listView = (ListView) this. findViewById (R. id. listview); adapter = new ListViewAdapter (this, infoList); listView. setAdapter (adapter);}/*** pull-down refresh */public void onRefresh () {new Handler (). postDelayed (new Runnable () {public void run () {swipeLayout. setRefreshing (false); ItemInfo info = new ItemInfo (); info. setName ("coin-refresh" + I); infoList. add (info); I ++; adapter. notifyDataSetChanged (); // refresh ListView }}, 500 );}}
6. Run the following instance:

Summary

1. There can only be one direct sub-View in this layout;

2. setOnRefreshListener (SwipeRefreshLayout. OnRefreshListener listener): sets the gesture slide listener;

3. Override public void onRefresh ().


Please pay attention to your favorite friends. Thank you!





 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.