Android Common Controls dropdown refresh WiFi list

Source: Internet
Author: User

Some list information needs to be manually updated, at this time the more commonly used is the drop-down refresh list, here you use the drop-down list to refresh the current WiFi information

Directory structure


Interface



Key code

Drop-down list class

Package Com.example.dropdownrefresh.ui;import Java.text.simpledateformat;import Java.util.date;import Com.example.dropdownrefresh.r;import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.View; Import Android.view.viewgroup;import Android.view.animation.linearinterpolator;import Android.view.animation.rotateanimation;import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.baseadapter;import Android.widget.ImageView; Import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.progressbar;import android.widget.textview;/** * drop-down list class * @author Administrator * */public class Dropdownlistview extends ListView implements Onscrolllistener {private static final String TAG = "ListView";p rivate final static int release_to_refresh = 0;private fin Al static int pull_to_refresh = 1;private final static int REfreshing = 2;private final static int done = 3;private final static int LOADING = 4;//The distance from the actual padding to the offset distance on the interface private fin Al static int RATIO = 3;private layoutinflater inflater;private linearlayout headview;private TextView Tipstextview;priva Te TextView lastupdatedtextview;private ImageView arrowimageview;private ProgressBar progressbar;private Rotateanimation animation;private rotateanimation reverseanimation;//is used to ensure that the value of Starty is recorded only once in a full touch event boolean isrecored;private int headcontentwidth;private int headcontentheight;private int starty;private int firstitemindex;private int State;private Boolean isback;private onrefreshlistener refreshlistener;private Boolean Isrefreshable;public Dropdownlistview (Context context) {super (context);//TODO auto-generated constructor Stubinit ( context);} Public Dropdownlistview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} private void init (context context) {Setcachecolorhint (Context.getresources (). GetColor (r.color.transparent)); Inflater = Layoutinflater.from (context); Headview = (linearlayout) inflater.inflate (r.layout.head, NULL); Arrowimageview = (ImageView) Headview.findviewbyid (R.id.head_arrowimageview); Arrowimageview.setminimumwidth (70); Arrowimageview.setminimumheight ();p Rogressbar = (progressBar) Headview.findviewbyid (R.id.head_progressbar); Tipstextview = (TextView) Headview.findviewbyid (r.id.head_tipstextview); Lastupdatedtextview = (TextView) Headview.findviewbyid (R.id.head_lastupdatedtextview); Measureview (headview); headcontentheight = Headview.getmeasuredheight (); headcontentwidth = Headview.getmeasuredwidth (); headview.setpadding (0,-1 * Headcontentheight, 0, 0); Headview.invalidate (); LOG.V ("Size", "width:" + headcontentwidth + "Height:" + headcontentheight); Addheaderview (Headview, NULL, FALSE); Setonscrolllistener (this); animation = new Rotateanimation (0, -180,rotateanimation.relative_to_self, 0.5f, Rotateanimation.relative_to_self, 0.5f); Animation.setinterpolator (new Linearinterpolator ()); ANIMation.setduration (+); Animation.setfillafter (true); reverseanimation = new Rotateanimation (-180, 0, Rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_to_self, 0.5f); Reverseanimation.setinterpolator ( New Linearinterpolator ()); Reverseanimation.setduration (n); Reverseanimation.setfillafter (true); state = done; Isrefreshable = false;} @Overridepublic void Onscroll (Abslistview arg0, int firstvisiableitem, int arg2,int arg3) {//TODO auto-generated method s Tubfirstitemindex = Firstvisiableitem;} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {//TODO auto-generated method Stub}public B Oolean ontouchevent (Motionevent event) {if (isrefreshable) {switch (event.getaction ()) {case Motionevent.action_down: if (Firstitemindex = = 0 &&!isrecored) {isrecored = True;starty = (int) event.gety (); LOG.V (TAG, "record current position at down time");} Break;case MotionEvent.ACTION_UP:if (state = refreshing && state! = LOADING) {if (state = = done) {//Do nothing}if (st ate = = Pull_To_refresh) {state = Done;changeheaderviewbystate (); LOG.V (TAG, "by the drop-down refresh state, to the done state");} if (state = = Release_to_refresh) {state = Refreshing;changeheaderviewbystate (); Onrefresh (); LOG.V (TAG, "by releasing the refresh state to the Done state");}} isrecored = False;isback = false;break;case MotionEvent.ACTION_MOVE:int tempy = (int) event.gety (); if (!isrecored &AMP;&A mp Firstitemindex = = 0) {log.v (TAG, "record position at Move"); isrecored = True;starty = Tempy;} if (state! = refreshing && isrecored && state! = LOADING) {//Ensure that the current position is always at head in the process of setting up padding, otherwise if the list exceeds the screen's , when the push is on, the list will scroll at the same time//can let go to refresh the IF (state = = Release_to_refresh) {setselection (0);//pushed up, pushed to the screen enough to cover the head of the extent, But it hasn't been pushed to the full cover. if ((tempy-starty)/RATIO < Headcontentheight) && (Tempy-starty) > 0) {state = Pull_to_re Fresh;changeheaderviewbystate (); LOG.V (TAG, "Change from release refresh State to dropdown refresh State"); Pushed to the top all at once else if (tempy-starty <= 0) {state = Done;changeheaderviewbystate (); LOG.V (TAG, "transition from released refresh state to done State");} Pulled down, or not pushed up to the top of the screen to hide the head of the other. {//No special action is necessary, only update paddingtThe OP value is on the line.}}//has not reached the show release refresh when done or Pull_to_refresh status if (state = = Pull_to_refresh) {setselection (0);//Pull down to enter Release_ To_refresh Status if ((Tempy-starty)/RATIO >= headcontentheight) {state = Release_to_refresh;isback = True;changeheaderv Iewbystate (); LOG.V (TAG, "changed from done or dropdown refresh state to release refresh"); Pushed up to the top of the else if (tempy-starty <= 0) {state = Done;changeheaderviewbystate (); LOG.V (TAG, "from done or drop-down refresh state to done State");}} Done status if (state = = do) {if (Tempy-starty > 0) {state = Pull_to_refresh;changeheaderviewbystate ();}} Update Headview's Sizeif (state = = Pull_to_refresh) {headview.setpadding (0,-1 * headcontentheight+ (tempy-starty)/RATIO, 0, 0);} Update Headview paddingtopif (state = = Release_to_refresh) {headview.setpadding (0, (tempy-starty)/Ratio-headcontenthei ght, 0, 0);}} Break;}} Return Super.ontouchevent (event);} /** * When the state changes, call the method to update the interface */private void Changeheaderviewbystate () {switch (state) {case Release_to_refresh: Arrowimageview.setvisibility (view.visible);p rogressbar.setvisibility (VIew. GONE); tipstextview.setvisibility (view.visible); lastupdatedtextview.setvisibility (view.visible); Arrowimageview.clearanimation (); arrowimageview.startanimation (animation); Tipstextview.settext ("Release Refresh"); LOG.V (TAG, "Current state, release refresh"); Break;case PULL_To_REFRESH:progressBar.setVisibility (View.gone); Tipstextview.setvisibility (view.visible); lastupdatedtextview.setvisibility (view.visible); Arrowimageview.clearanimation (); arrowimageview.setvisibility (view.visible);//is a transition from the Release_to_refresh state to the IF ( Isback) {isback = False;arrowimageview.clearanimation (); arrowimageview.startanimation (reverseAnimation); Tipstextview.settext ("drop-down Refresh");} else {tipstextview.settext ("drop-down Refresh");} LOG.V (TAG, "Current state, drop-down refresh"); Break;case REFRESHING:headView.setPadding (0, 0, 0, 0);p rogressbar.setvisibility ( view.visible); arrowimageview.clearanimation (); arrowimageview.setvisibility (View.gone); TipsTextview.setText (" refreshing ... "); lastupdatedtextview.setvisibility (view.visible); LOG.V (TAG, "current state, refreshing ..."); Break;case DONE:headView.setPadding (0,-1 * HeAdcontentheight, 0, 0);p rogressbar.setvisibility (View.gone); Arrowimageview.clearanimation (); Arrowimageview.setimageresource (R.drawable.arrow); Tipstextview.settext ("drop-down refresh"); Lastupdatedtextview.setvisibility (view.visible); LOG.V (TAG, "current state, done"); public void Setonrefreshlistener (Onrefreshlistener refreshlistener) {this.refreshlistener = Refreshlistener; Isrefreshable = true;} Public interface Onrefreshlistener {public void Onrefresh ();} public void Onrefreshcomplete () {state = done; SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Lastupdatedtextview.settext ("Last Updated:" + Sdf.format ( New Date ())); Changeheaderviewbystate ();} private void Onrefresh () {if (Refreshlistener! = null) {Refreshlistener.onrefresh ();}} /** * This method is copied directly from a drop-down refresh on the network demo, here is "estimate" headview width and Height * @param child */private void Measureview (View child) { Viewgroup.layoutparams p = child.getlayoutparams (), if (p = = null) {p = new Viewgroup.layoutparams (viewgroup.layoutparams . Match_parent,viewgroup.layoutparams.wrap_content);} int childwidthspec = viewgroup.getchildmeasurespec (0, 0 + 0, p.width); int lpheight = P.height;int childheightspec;if (lpHe ight > 0) {childheightspec = Measurespec.makemeasurespec (lpheight,measurespec.exactly);} else {childheightspec = Measurespec.makemeasurespec (0,measurespec.unspecified);} Child.measure (Childwidthspec, childheightspec);} public void Setadapter (Baseadapter adapter) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Lastupdatedtextview.settext ("Last Updated:" + sdf.format (new Date ())); Super.setadapter (adapter);}}
Main.xml

<linearlayout 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:background= "@drawable/bg"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <com.example.dropdownrefresh.ui.dropdownlistview        android:id=" @+id/listview "        Android:layout_width= "Match_parent"        android:layout_height= "match_parent"        android:cachecolorhint= "# 00000000 "        android:childdivider=" @drawable/list_driver "        android:divider=" @drawable/list_driver "/> </LinearLayout>
Drop-down header layout Head.xml

<?xml version= "1.0" encoding= "Utf-8"?><!--the head of the ListView--><linearlayout xmlns:android= "/http/    Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "> <!--Content-<relativelayout android:id= "@+id/head_contentlayout" android:layout_width= "match_p        Arent "android:layout_height=" wrap_content "android:paddingleft=" 30DP "> <!--arrow image, progress Bar--            <framelayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentleft= "true" android:layout_centervertical= "true" > <!--arrow--&            Gt                <imageview android:id= "@+id/head_arrowimageview" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" Android:cont entdescription= "@null"               android:src= "@drawable/arrow"/> <!--progress Bar--<progressbar Android:id= "@+id/head_progressbar" style= "Android:attr/progressbarstylesmall" Android:layo                 Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:visibility= "Gone"/> </FrameLayout> <!--tips, latest update-<linea Rlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Layout_centerhorizontal= "true" android:gravity= "Center_horizontal" android:orientation= "vertical" &G            T <!--tips-<textview android:id= "@+id/head_tipstextview" android:layout                _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "drop-down Refresh" Android:Textcolor= "@color/white" android:textsize= "20sp"/> <!--Last updated--&LT;TEXTVI                EW android:id= "@+id/head_lastupdatedtextview" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Last Updated" android:textcolor= "@color/gold "Android:textsize=" 12sp "/> </LinearLayout> </relativelayout></linearlayout&gt ;
When the drop-down list is released, you need to scan WiFi and refresh the list

Package Com.example.dropdownrefresh;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Android.app.activity;import Android.net.wifi.scanresult;import Android.os.asynctask;import Android.os.bundle;import Android.view.view;import Android.view.viewgroup;import Android.widget.BaseAdapter;import Android.widget.linearlayout;import Android.widget.textview;import Com.example.dropdownrefresh.ui.dropdownlistview;import Com.example.dropdownrefresh.ui.dropdownlistview.onrefreshlistener;import com.example.dropdownrefresh.utils.wifiadmin;/** * Use the dropdown refresh search WiFi * @author Administrator * */public class Mainactivity Extends Activity {wifiadmin wifiadmin = null;private arraylist

Related code: http://download.csdn.net/detail/deng0zhaotai/7969215

Android Common Controls dropdown refresh WiFi list

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.