Android custom control in the ListView pull-up load with drop-down refresh

Source: Internet
Author: User

Custom Control Loadlayout

ImportAndroid.content.context;importAndroid.graphics.drawable.animatable;importAndroid.graphics.drawable.drawable;importAndroid.util.attributeset;importAndroid.view.layoutinflater;importAndroid.view.motionevent;importAndroid.view.view;importAndroid.view.viewconfiguration;importAndroid.widget.abslistview;importAndroid.widget.framelayout;importAndroid.widget.listview;importAndroid.widget.textview;importcom.xh.boke.r;/** * Custom Control ListView Pull-load * Created by Administrator on 2015/10/22 0022. */public class Loadlayout extends Framelayout implementsAbslistview.onscrolllistener {Private enumOperation {START, STOP}/** * Swipe to the bottom of the pull-up operation */private intMtouchslop; /** * ListView Instance */PrivateListView Mlistview; /** * Bottom Text */privateTextView Mfoottext; /** * Pull-up listener, to the bottom of the pull-up loading operation */PrivateOnloadlistener Monloadlistener; /** * ListView Loading in footer */privateView Mlistviewfooter; /** * y-coordinate when pressed */private intMydown; /** * The y-coordinate of the lift, when used with the mydown to swipe to the bottom to determine if it is pull-up or drop-down */private intMlasty; /** * Whether in load (pull up more) */private Boolean isloading = False; /** * @param context */publicLoadlayout (Context context) {This (context, NULL); } publicLoadlayout (context context, AttributeSet Attrs) {Super(context, attrs); Mtouchslop =Viewconfiguration.get (context). Getscaledtouchslop (); Mlistviewfooter = Layoutinflater.from (context). Inflate (R.layout.view_lv_footer, NULL, FALSE); Mfoottext =(TextView) Mlistviewfooter.findviewbyid (R.id.pull_to_refresh_loadmore_text); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, intBottom) {Super. OnLayout (changed, left, top, right, bottom); Initializes the ListView object if (Mlistview = = NULL) {Getlistview ();}} /** * Get ListView Object */private voidGetlistview () {int childs =Getchildcount (); if (Childs > 0{for (int i = 0; i < childs; i++) {View Childview =Getchildat (i); if (Childview instanceofListView) {Mlistview =(ListView) Childview; Set the scroll listener to the ListView so that scrolling can also automatically load the Mlistview.setonscrolllistener (this); }}}}/* * (non-javadoc) * @see android.view.viewgroup#dispatchtouchevent (android.view.MotionEvent) */@Override Public BooleanDispatchtouchevent (Motionevent event) {Final int action =Event.getaction (); Switch(action) {caseMotionevent.action_down://press Mydown = (int) Event.getrawy (); Break; CaseMotionevent.action_move://Move break; CaseMotionEvent.ACTION_UP:mLastY = (int) Event.getrawy (); Lift up if(Canload ()) {LoadData ();} Break; Default: Break; } return Super. Dispatchtouchevent (event); }/** * Can load more, the condition is at the bottom, the ListView is not loaded, and is a pull-up operation. * * @return * * Private BooleanCanload () {return isbottom () &&!isloading &&Ispullup (); }/** * To determine if the bottom */private BooleanIsbottom () {if (Mlistview! = null && mlistview.getadapter ()! = NULL) {return mlistview.getlastvisibleposition () = = (Mlistview.getadapter (). GetCount ()-1); } return False; }/** * is a pull-up operation * * @return */Private BooleanIspullup () {return (mydown-mlasty) >=Mtouchslop; }/** * If it is at the bottom and is a pull-up operation. Then execute the OnLoad method */private voidLoadData () {if (Monloadlistener! = null) {//Set state setloading (True); //Monloadlistener.onload (); }}/** * @param loading */public void setloading (BooleanLoading) {isloading =Loading If(isloading) {changeanimation (Operation.start); Mlistview.addfooterview (Mlistviewfooter, NULL, FALSE); } else{changeanimation (operation.stop); Mlistview.removefooterview (mlistviewfooter); mydown = 0; Mlasty = 0; }}/** * @param loadlistener */public voidSetonloadlistener (Onloadlistener loadlistener) {Monloadlistener =Loadlistener;} @Override public void onscrollstatechanged (Abslistview view, Int. scrollstate) {} @Override public void Onscroll (Absli Stview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {//scroll up to the bottom and load more if (Canload ( ) {LoadData ();}} /** * Load More Listeners * * @author mrsimple */public static interface Onloadlistener {public void onLoad ();} private Vo ID changeanimation (Operation operation) {drawable[] drawables = mfoottext.getcompounddrawables (); for ( Drawable drawable:drawables) {if (drawable! = null && drawable instanceof animatable) {animatable animata ble = ((animatable) drawable), switch (operation) {case START:animatable.start (), break, Case S TOP:animatable.stop (); Break;}} } }}

View_lv_footer.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" Wrap_content "    android: Background= "#ffffff"    android:gravity= "center"    android:paddingbottom= "8dip"    android:paddingtop= " 5dip ">    <textview        android:id=" @+id/pull_to_refresh_loadmore_text "        android:layout_width=" Wrap_ Content "        android:layout_height=" wrap_content "        android:layout_gravity=" center "        android:gravity=" Center "        android:paddingtop=" 5dip "        android:drawableleft=" @anim/rotating_loading "        android: drawablepadding= "8DP"        android:text= "Loading ..."        android:textappearance= "? android:attr/ Textappearancemedium "        android:textcolor=" @android: Color/black "        android:textsize=" @dimen/activity_ Horizontal_margin "        android:textstyle=" bold "/></relativelayout>

Rotating_loading.xml

<?xml version= "1.0" encoding= "Utf-8"? ><animated-rotate  xmlns:android= "http://schemas.android.com/ Apk/res/android "  android:pivotx=" 50% "  android:pivoty=" 50% "  android:drawable=" @mipmap/ic_ref "  android:duration= "Ten"/>

Use the method (implements Loadlayout.onloadlistener first, implement the load operation in the OnLoad () method)

Setcontentview (R.layout.activity_main); Loadlayout load = (loadlayout) Findviewbyid (r.id.load); Load.setonloadlistener (this);

Close loading

Load.setloading (FALSE);

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><com.xh.boke.component.loadlayout xmlns:android= "/http Schemas.android.com/apk/res/android "    android:id=" @+id/load "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent" >    <listview        android:id= "@+id/lv"        android:layout_width= " Match_parent "        android:layout_height=" Match_parent "/></com.xh.boke.component.loadlayout>

Android custom control in the ListView pull-up load with drop-down refresh

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.