Drop-down refresh: Inherit the ListView control

Source: Internet
Author: User
Tags gety

1, the first time to initialize the control to listen to the Onscrolllistener, wherein the onscroll parameters to get the first display of the entry, when the first entry is 0, you can perform the next refresh. The second way is to write

Onscrollstatechanged is a sliding state of listening, 3 states are constants: fast sliding, touch move, idle state, when the second time call Getlastvisiableposition () to get the last entry displayed, When the number of total entries-1 o'clock can be loaded more than the header with the foot when initializing. Displays foot when the last one is displayed and loads the data.

1 down the refresh time, you can add a head to the ListView, in order to start hiding the head must know the height of the head, call the Setpadding method, but do not know the height of the view when the control is defined, manually call the Header.measure () method to measure the height of the exception occurs, Because there is no indication that it cannot be measured (personally, it is measured in the earlier video where the definition was found). We can use measure and setpadding to hide the header of the ListView immediately after the main program is loaded (this article is handled in the control definition after the main program is obtained, if implemented in the main program, Both Ontouchevent and Onscrolllistener are implemented in the main program). Other interfaces and the like with the previous article, not much to say. the same approach can be implemented: Load more entries at the bottom of the row.

Note: The simple design of the method of the touch movement, without monitoring if the start 1 is invisible, from the top to the bottom to 1 appears and then refreshed, which did not note the downy, so the best way is to give the screen to set 3 states: 1 and the last item is not visible; see 1; see the State of the last article. Once you see 1, write down as downy, if you see the last one is Upy, then the refresh and loading more are divided into three states, the following example: down, pull to greater than half release refresh, pull to a certain distance up.

2, in fact, each space is calculated after the slide to the screen, respectively, through measure, OnLayout (), OnDraw three layers, each control is the size of its parent view and itself, not to mention here to see the following articles: Http://blog.csdn.net /fengye810130/article/details/9181531;http://blog.csdn.net/guolin_blog/article/details/16330267;http:// blog.csdn.net/cauchyweierstrass/article/details/41317247

The following is a custom drop-down refresh operation:

3 Give the Code

XML file

<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=" com.example.refresh.mainactivity$placeholderfragment ">    <com.example.refresh.mylistview.mylistview        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:id= "@+id/mylist"         /></relativelayout>

Header's File

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout 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" > <progressbar android:id= "@+id/progressbar1" android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:layout_margintop= "3DP" android:visibility= "Invisible"/> <imageview android:id= "@+id/arrow" Android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/common_listview_headvi         Ew_red_arrow "android:layout_marginleft=" 20DP "android:layout_margintop=" 3DP "/> <textview Android:id= "@+id/tev1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andro Id:layout_centerhorizontal= "true"android:layout_margintop= "15DP" android:text= "dropdown refresh"/><textview android:id= "@+id/tev2" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/tev1" Android:layout_c Enterhorizontal= "true"/> </RelativeLayout>

3 Control Definition:

Package Com.example.refresh.mylistview;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.date;import Com.example.refresh.r;import Android.content.context;import Android.graphics.Bitmap;import Android.graphics.bitmapfactory;import Android.net.rtp.rtpstream;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.rotateanimation;import Android.widget.AbsListView ; Import Android.widget.listview;import Android.widget.abslistview.onscrolllistener;import Android.widget.progressbar;import Android.widget.textview;public class Mylistview extends ListView implements Onscrolllistener{private Context context;private final static int up_move=2;private final static int prepare_flashing=1; Private final static int down_move=0;public View header;private int firstcellindex;private int headhignt=100;private Load Dataslistener Loaddataslistener;p ublic void Setonloaddataslistener (Loaddataslistener l) {this.loaddataslistener=l;} Public Mylistview (Context context) {super (context); This.context=context;init ();} Public Mylistview (context context, AttributeSet Attrs) {Super (context, attrs); This.context=context;init ();} Public Mylistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); this.context= Context;init ();} public void init () {AddHeader ();//int h=header.getheight ();//measurement occurs abnormally//system.out.println (h); Setonscrolllistener ( This)///Listen for sliding events, always find the first displayed control, that is, the second parameter of Onscroll. }int Downy=0;int Currenty=0;int dis=0;//Remember the last move distance @overridepublic boolean ontouchevent (motionevent ev) {if ( firstcellindex==0) {switch (ev.getaction ()) {case motionevent.action_down:downy= (int) ev.gety (); ProgressBar bar2= (ProgressBar) Header.findviewbyid (R.ID.PROGRESSBAR1); bar2.setvisibility (INVISIBLE); Header.findviewbyid (R.id.arrow). Setbackgroundresource (R.drawable.common_listview_headview_red_arrow); Header.findviewbyid (r.id.arrow). Setvisibility (VISIBLE);((TextView) Header.findviewbyid (R.ID.TEV1)). SetText ("Down Refresh"); Break;case Motionevent.action_move: currenty= (int) ev.gety (), int dis2=currenty-downy;//pad value int dis3=currenty-downy;//actual disif (dis2>=headhignt) {Dis2 =headhignt; ((TextView) Header.findviewbyid (R.ID.TEV1)). SetText ("release Refresh");} Header.setpadding (0,-headhignt+dis, 0, 0); if (Dis3>=dis) Header.findviewbyid (R.id.arrow). Setbackgroundresource (R.drawable.common_listview_headview_red_arrow); if (Dis3<dis) {// Header.findviewbyid (R.id.arrow) when moving up. Setbackgroundresource (r.drawable.up);} Dis=dis3;header.setpadding (0, dis2-headhignt, 0, 0); Break;case MotionEvent.ACTION_UP:Calendar C = Calendar.getinstance (); int year = C.get (calendar.year); int month = C.get (calendar.month) +1;//Month is an int starting from 0, date = c.get (calendar.date); int hour = C.get (Calendar.hour_of_day); int minute = C.get (Calendar.minute); String time= "Last refreshed:" +year+ "-" +month+ "-" +date+ "" +hour+ ":" +minute; TextView t= (TextView) Header.findviewbyid (R.ID.TEV2); T.settext (time); ProgressBar Bar= (ProgressBar) Header.findviewbyid (R.ID.PROGRESSBAR1); bar.setvisibility (view.visible); Header.findviewbyid ( R.id.arrow). Setvisibility (INVISIBLE);((TextView) Header.findviewbyid (R.ID.TEV1)). SetText ("refreshing"); if (dis> HEADHIGNT/2) header.setpadding (0, 0, 0, 0); else{header.setpadding (0,-headhignt, 0, 0); Loaddataslistener.onloaddata () ;} Dis=0;break;default:break;}} return super.ontouchevent (EV);} public void AddHeader () {header=view.inflate (context, r.layout.header, null);//header.measure (0, 0);// System.out.println (This.getmeasuredheight ()); Addheaderview (header); header.setpadding (0,-headhignt, 0, 0);} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {//TODO auto-generated method stub}@ overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) { Firstcellindex=firstvisibleitem;}}

4 Control Interface:

Package Com.example.refresh.mylistview;public interface Loaddataslistener {public void onloaddata ();

5 Activity files

public class Mainactivity extends Activity {private Mylistview list;private arraylist<string> newsdata;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        List= (Mylistview) Findviewbyid (r.id.mylist);        Init ();        List.setadapter (New Mylistadapter ());        List.measure (0, 0);//List.header.getMeasuredHeight ();            System.out.println (List.header.getMeasuredHeight ());  } class Mylistadapter extends Baseadapter {@Overridepublic int getcount () {//TODO auto-generated method Stubreturn Newsdata.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn newsdata.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {TextView view=null; Viewholder Holder=null;if (converTview!=null) {holder= (Viewholder) Convertview.gettag (); view=holder.txview;} else {view=new TextView (mainactivity.this); holder=new Viewholder (); Holder.txview=view;view.settag (holder);}        View.settext (Newsdata.get (position)); return view;}    } static class Viewholder {TextView txview;    } public void Init () {newsdata=new arraylist<string> ();        for (int x=0;x<30;x++) Newsdata.add ("News entry:" +x); }   }

  

Drop-down refresh: Inherit the ListView control

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.