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

Source: Internet
Author: User
Tags message queue

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 easy to use: Take a look at some of the most important ways to use the document, no matter what.


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvsxrkyxzhd2zj/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


Case 1:

Main related code

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);}}

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

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>


Case TWO:

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

Adapter

public class Listviewadapter extends Baseadapter {/** environment information **/private Context context;/** User Center information collection **/private ARRAYLIST&LT ; 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;}}
entity class:

public class Softwareclassificationinfo {/** class number **/private int cid;/** class name **/private String catname;public int Getcid ( ) {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;}}

Main activity

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. Load an outer swiperefreshlayoutswipelayout = (swiperefreshlayout) Findviewbyid (R.id.swipe_container) from the layout with a refreshed ListView;// 2. Set the Refresh Listener Swipelayout.setonrefreshlistener (this) for the ListView with Refresh,//3. The load color is played in a loop, just to not complete the refresh 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 ListView in Listviewlistview = (ListView) Findviewbyid (r.id.list); adapter = new ListViewAdapter (this, list); Listview.setadapter (Adapter);/* Personal Summary: ListView-----Adapter * First regardless of Adapter is Adapter type "aggregation Type" * Implement adapter's ListView at least: Data "entity: JavaBean", aggregation "to hold entity aggregation", layout ListView or ListView Related Subclass "* Adapter" for storing 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);}}

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 handler, here again to summarize: 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. It is the MessageQueue (Message Queuing) that manages this line of thread.


2) Handler: You can construct Handler objects to communicate with Looper. 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. Android starts a program and builds a MessageQueue for it.
1.Handler Creating messages
Each message needs to be processed by the specified handler, which can be completed by handler creating the message. The message pool is introduced in the Android messaging mechanism.

Handler when a message is created, the message pool is first queried for the presence of a message, assuming that it is obtained directly from the message pool, assuming that there is not yet another instance of the message initialized.

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 is seen.

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. So far. The relationship between the main thread, message loop, and message queue is 1:1:1.


Handler, Looper, and MessageQueue are seen in the initialization process:

Hander holds a reference to the UI main thread message queue MessageQueue and message loop Looper, which can send messages to the UI thread's message queue MessageQueue through handler.
3.Handler processing messages
The UI main thread queries the message queue UI_MQ through the Looper loop. The message is removed from the message queue when a message is found to exist.

The message is parsed first, the corresponding handler of the message is inferred from the parameters of the message, and the message is then 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.