Android swiperefreshlayout Official dropdown Refresh Control Introduction-handler Principle-adapter Summary

Source: Internet
Author: User

Reference Blog: http://blog.csdn.net/lmj623565791/article/details/24521483

I knocked on the code, this way refreshing really refreshing feeling. Resources: contains two swiperefreshlayout actual demo,http://download.csdn.net/detail/itjavawfc/8847339

It's very easy to use: Take a look at the documentation for several important methods and simply use it without any problems.




Case 1:

Main related code

<span style= "FONT-SIZE:18PX;" >public class Mainactivity extends Activity implements swiperefreshlayout.onrefreshlistener{private static final int Refresh_complete = 0x110;private swiperefreshlayout mswipelayout;private ListView mlistview;private ArrayAdapter< string> madapter;private list<string> mdatas = new Arraylist<string> (arrays.aslist ("Java", "Javascript "," C + + "," Ruby "," Json "," HTML "));p rivate Handler mhandler = new Handler () {public void Handlemessage (android.os.Message m SG) {switch (msg.what) {case REFRESH_COMPLETE:mDatas.addAll (arrays.aslist ("Lucene", "Canvas", "Bitmap")); Madapter.notifydatasetchanged (); mswipelayout.setrefreshing (false); break;};}; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Mlistview = (ListView) Findviewbyid (r.id.id_listview); mswipelayout = (swiperefreshlayout) Findviewbyid (R.id.id_swipe_ly); Mswipelayout.setonrefreshlistener (this); Mswipelayout.setcolorscheme (Android. R.color.holo_green_dark, Android. R.color.holo_green_light,android. R.color.holo_orange_light, Android. R.color.holo_red_light); madapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, Mdatas); Mlistview.setadapter (Madapter);} public void Onrefresh () {mhandler.sendemptymessagedelayed (Refresh_complete, 2000);}} </span>

The layout is as follows: basically all layouts are a refreshed layout

<span style= "FONT-SIZE:18PX;" >android.support.v4.widget.swiperefreshlayout<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Surrounded by </span></span>

<span style= "FONT-SIZE:18PX;" ><relativelayout 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/id_swipe_ly"        android:layout_width= "match _parent "        android:layout_height=" match_parent ">        <listview            android:id=" @+id/id_listview            " Android:layout_width= "Match_parent"            android:layout_height= "match_parent" >        </ListView>    </android.support.v4.widget.SwipeRefreshLayout></RelativeLayout></span>


Case TWO:

The adapter adapter is used to match relevant data for better presentation: "

Adapter

<span style= "FONT-SIZE:18PX;" >public class Listviewadapter extends Baseadapter {/** environment information **/private Context context;/** User Center information collection **/private Arraylis t<softwareclassificationinfo> list;/** Construction **/public listviewadapter (context context, arraylist< Softwareclassificationinfo> list) {this.context = Context;this.list = list;} public int GetCount () {//TODO auto-generated method Stubreturn list.size ();} Public Object getItem (int arg0) {//TODO auto-generated method Stubreturn null;} public long getitemid (int arg0) {return 0;} Public View GetView (int position, View Convertview, ViewGroup arg2) {_view View = null;if (Convertview = = null) {View = NE W _view (); Convertview = Layoutinflater.from (context). Inflate (R.layout.user_center_list_item, null); View.list_text = (TextView) Convertview.findviewbyid (r.id.user_center_item_txt); view.list_img = (ImageView) Convertview.findviewbyid (r.id.user_center_item_img); Convertview.settag (view);} else {view = (_view) Convertview.gettag ();} View.list_texT.settext (List.get (position). Getcatname ());//view.list_img.setbackgrounddrawable (Context.getresources (). Getdrawable (//r.drawable.tool_box_fragment_settings_icon)); View.list_img.setBackgroundResource (R.drawable.tool _box_fragment_settings_icon); return convertview;} Class _view {TextView List_text;imageview list_img;}} </span>

Entity class:

<span style= "FONT-SIZE:18PX;" >public class Softwareclassificationinfo {/** class number **/private int cid;/** category name **/private String catname;public int get CID () {return CID;} public void Setcid (int cid) {this.cid = CID;} Public String Getcatname () {return catname;} public void Setcatname (String catname) {this.catname = CatName;} Public Softwareclassificationinfo () {//TODO auto-generated constructor stub}public softwareclassificationinfo (int cid , String catname) {//TODO auto-generated constructor stubthis.cid = Cid;this.catname = CatName;}} </span>

Main activity

<span style= "FONT-SIZE:18PX;" >public class Swiprefreshlayoutactivity extends Activity implementsswiperefreshlayout.onrefreshlistener {private Swiperefreshlayout swipelayout;private ListView listview;private listviewadapter adapter;private ArrayList< softwareclassificationinfo> list; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.swipe_refresh_layout);//1. Loads an outer swiperefreshlayoutswipelayout = (swiperefreshlayout) Findviewbyid (R.id.swipe_container) of a ListView with a refresh from the layout;// 2. Set Refresh Listener Swipelayout.setonrefreshlistener (this) for this ListView with Refresh,//3. The load color is looped, as long as the refresh does not complete, it will always loop, Color1>color2 >color3>color4swipelayout.setcolorscheme (Android. R.color.white,android. R.color.holo_green_light,android. R.color.holo_orange_light, Android. r.color.holo_red_light); list = new arraylist<softwareclassificationinfo> (); List.add (new Softwareclassificationinfo (1, "Asdas"));//Get this refreshed in the ListView Listviewlistview = (ListView) findviewbyiD (r.id.list); adapter = new Listviewadapter (this, list); Listview.setadapter (adapter);/* Personal Summary: ListView-----Adapter * First, no matter adapter is the type of adapter, "Aggregation type" * Implements the adapter ListView at least: Data "entity: JavaBean", aggregate "to hold entity aggregation", Layout listview, or ListView related subclass " The "* adapter" that is used to store the class capacity you use to fit a clustered adapter ", let the layout and adapter relate" Setadapter "*/}public void Onrefresh () {new Handler (). postdelayed ( New Runnable () {public void run () {swipelayout.setrefreshing (false); List.add (New Softwareclassificationinfo (2, "the") ); adapter.notifydatasetchanged ();}, 3000);}} </span>

Summary:

I. Swiprefreshlayout main methods:

Setonrefreshlistener (Onrefreshlistener): Add a listener to the layout
Setrefreshing (Boolean): Show or hide the Refresh progress bar
Isrefreshing (): Check whether it is in a refreshed state
SetColorScheme (): Set the color theme of the progress bar, can set up to four kinds of
Two. Personal Summary: ListView-----Adapter
* First, regardless of adapter is the type of adapter "aggregation type"
* Implement adapter's ListView at least: Data "entity: JavaBean", aggregation "to hold entity aggregation", layout ListView or ListView related subclass "for storing class capacity"
* Adapter "You used to fit a clustered adapter", let the layout and adapter Connect "Setadapter"
three. Case one used to handler, here again summed up: can look at the source code, very clear. Andriod provides handler and Looper to meet the communication between threads. Handler first-out principle. The Looper class is used to manage message exchange (Messageexchange) between objects within a particular thread.

1) Looper: A thread can produce a Looper object that manages the MessageQueue (Message Queuing) of this line thread.
2) Handler: You can construct a Handler object to communicate with Looper in order to push a new message into the MessageQueue, or to receive a message sent by Looper from the message queue.
3) Message queue (Message Queuing): Used to hold messages placed by the thread.
4) Thread: Uithread is usually the main thread, and Android initiates a program to create a MessageQueue for it.
1.Handler Creating messages
Each message needs to be handled by the specified handler, and the message can be created by handler to complete this function. The message pool is introduced in the Android messaging mechanism. Handler when a message is created, it first queries whether a message exists in the message pool, if one is obtained directly from the message pool, and if not, reinitialize a message instance. The advantage of using a message pool is that when a message is not being used, it is not garbage collected, but instead is put into a message pool that can be used the next time handler creates a message. The message pool improves the reuse of message objects and reduces the number of system garbage collections. The creation process of the message.

2.Handler sending messages
When the UI main thread initializes the first handler, a looper is created through threadlocal, which corresponds to the UI main thread one by one. The purpose of using threadlocal is to ensure that each thread creates only one looper. After the other handler initialization, the first handler created Looper is obtained directly. A message queue MessageQueue is created when the Looper is initialized. At this point, the relationship between the main thread, message loop, and message queue is 1:1:1.
Handler, Looper, MessageQueue initialization process:

hander holds a reference to the UI main thread message queue MessageQueue and message loop Looper, which can be sent by handler to Message Queuing MessageQueue of the UI thread.
3.Handler processing messages
The main thread of the UI queries the message queue UI_MQ through Looper, and the message is removed from the message queue when a message is found to exist. The message is parsed first, the message's corresponding handler is judged by its parameters, and the message is distributed to the specified handler for processing.
The process by which a child thread communicates through handler, Looper, and the UI main thread.











Android swiperefreshlayout Official dropdown Refresh Control Introduction-handler Principle-adapter Summary

Related Article

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.