Android: Pull down refresh + load more + slide Delete instance explain _android

Source: Internet
Author: User
Tags stub visibility

Small partners in Taobao or a variety of apps, you can see such a function, drop refresh and load more as well as slide delete, refresh, after brushing to make it new, analogy to break the old and create new, such as on the mobile phone to browse the news, use the Drop-down refresh function, we can first time to grasp the latest news, Loading more is what NIE, in simple terms is on the Web page taobao, we can click on the next page to meet more of our needs, but in the phone end is not the same, there is no page, how to do nie, the method is always more than difficult, attentive little partner may find, in the mobile phone end, There are loads more to meet our requirements, in fact, loading more is also a reflection of pagination. Small partners in the use of mobile phone version of QQ, the message interface will have a lot of information, if you are like a small series of obsessive-compulsive disorder, want to read the message to delete, how to do nie,qq to the right to slide the deletion of the function, recently in the small series of the project, on the face of such a function Drop-down refresh and load more as well as sliding delete, After a few days of tinkering, when the second ray of Winter sun shines on the small series, small knitting finally put her out, today this blog, small knitting Main and small partners share Drop-down refresh and load more sliding delete, hope to help to have the need of small partners, but also ask the small partners a lot of advice.

First we need to write the contents of XML, we named Activity_item, the specific code is as follows:

xml/html Code

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
  android:id= "@+id/ll_parent" 
  android:layout_width= "match_parent" 
  android:layout_height= "match_parent" 
  android:background= "@ Android:color/darker_gray " 
  android:orientation=" vertical "> 
 
  <textview 
    android:id=" @+id/tv_ Text " 
    android:layout_width=" match_parent " 
    android:layout_height=" match_parent " 
    android:gravity=" Center " 
    android:textsize=" 25sp "/> 
 
</LinearLayout>

Next, we'll write code in List_item about XML, as shown in the following code:

xml/html Code

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:id=" @+id/ll_parent "android:layout_width=" Match_parent "android:layout_height=" "Match_parent" android:gravity= "center" android:background= "@android: Color/darker_gray" tools:context= ". 
    Mainactivity "> <textview android:id=" @+id/list_item_textview "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_centervertical= "true" android:textcolor= "#000" Andro Id:textsize= "16sp"/> <framelayout android:layout_width= "wrap_content" android:layout_height= "Match_p" Arent "android:layout_centervertical=" true "android:layout_alignparentright=" true "android:padding=" 15DP "& 
 
    Gt 
      <textview android:id= "@+id/tv_functions" android:layout_width= "90DP" android:layout_height= "50DP" 
  android:layout_gravity= "Center"    android:background= "@drawable/btn_del_bg" android:gravity= "center" android:textcolor= "@android: Color/whi TE "android:text=" deletes "/> <textview android:id=" @+id/tv_coating "android:layout_width=" 9 0DP "android:layout_height=" 50DP "android:background=" @android: Color/darker_gray "Android:layout_grav 
 ity= "center" android:visibility= "Gone"/> </FrameLayout> </RelativeLayout>

In the third step, we'll write the XML file in main, as shown in the following code:

xml/html Code

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
  android:layout_width= "Fill_ Parent " 
  android:layout_height=" fill_parent " 
  android:background=" #f0f0f0 " 
  android:orientation=" Vertical "> 
 
  <textview 
    android:layout_width=" fill_parent "android:layout_height=" Wrap_content " 
    android:text= "@string/hello"/> 
 
  <me.maxwin.view.xlistview 
    android:id= "@+id/xlistview 
    " Android:layout_width= "Fill_parent" 
    android:layout_height= "fill_parent" 
    android:cachecolorhint= "# 00000000 "> 
  </me.maxwin.view.XListView> 
 
</LinearLayout> 

The fourth step is to write the XML file inside the Xlistview_footer, as shown in the following code:

xml/html Code

 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android : layout_width= "fill_parent" android:layout_height= "wrap_content" > <relativelayout android:id= "@+id/xli" Stview_footer_content "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:padd" ing= "10DP" > <progressbar android:id= "@+id/xlistview_footer_progressbar" android:layout_width= " Wrap_content "android:layout_height=" Wrap_content "android:layout_centerinparent=" true "Android:visib ility= "Invisible"/> <textview android:id= "@+id/xlistview_footer_hint_textview" Android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" Androi d:text= "@string/xlistview_footer_hint_normal"/> </RelativeLayout> </LinearLayout> 

The fifth step is to write the XML file inside the Xlistview_header, as shown in the following code:

xml/html Code

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:gravity= "Bottom" > <relativelayout android:id= "@+id/xlistv"  
      Iew_header_content "android:layout_width=" fill_parent "android:layout_height=" 60DP "> <linearlayout Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_centerinpare Nt= "true" android:gravity= "center" android:orientation= "vertical" android:id= "@+id/xlistview_header_text" 
 
      ; 
        <textview android:id= "@+id/xlistview_header_hint_textview" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/xlistview_header_hint_normal"/> <Li Nearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:lay Out_margintop= "3dP "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_cont Ent "android:text=" @string/xlistview_header_last_time "android:textsize=" 12SP "/> < TextView android:id= "@+id/xlistview_header_time" android:layout_width= "Wrap_content" Andr oid:layout_height= "Wrap_content" android:textsize= "12sp"/> </LinearLayout> </linearlay 
      out> <imageview android:id= "@+id/xlistview_header_arrow" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@id/xlistview_header_text" android:layou 
 
    T_centervertical= "true" android:layout_marginleft= " -35DP" android:src= "@drawable/xlistview_arrow"/> <progressbar android:id= "@+id/xlistview_header_progressbar" android:layout_width= "30DP" Android 
 : layout_height= "30DP"     android:layout_alignleft= "@id/xlistview_header_text" android:layout_centervertical= "true" android:layou 
 t_marginleft= " -40DP" android:visibility= "invisible"/> </RelativeLayout> </LinearLayout>

So far, the code for our layout XML file has been written, and then we start writing the code inside the Java class, and first, we'll write the code inside the Itemactivity, as shown in the following code:

Java code

Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnTouchListener; 
 
Import Android.widget.TextView; 
 
  public class Itemactivity extends activity implements Ontouchlistener {private float x, UPX; 
    protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_item); 
  Init (); 
    private void Init () {Findviewbyid (r.id.ll_parent). Setontouchlistener (this); 
  ((TextView) Findviewbyid (R.id.tv_text)). SetText (Getintent (). Getstringextra ("item")); 
      public boolean Ontouch (View V, motionevent event) {if (event.getaction () = = Motionevent.action_down) { 
    x = Event.getx (); 
      } if (event.getaction () = = motionevent.action_up) {UPX = Event.getx (); 
        if (Math.Abs (X-UPX) >) {this.finish (); 
 Overridependingtransition (0, r.anim.slide_out_to_right);     } return true; 
 } 
 
}

Next, we'll write the code inside the adapter adapter, as shown in the following code:

Java code

Import java.util.ArrayList; 
Import Android.content.Context; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
 
Import Android.widget.TextView; 
  public class Itemadapter extends Baseadapter {private Layoutinflater inflater; 
 
  Private arraylist<string> datas; 
  Public Itemadapter {inflater = Layoutinflater.from (context); 
  public void SetData (arraylist<string> datas) {this.datas = datas; 
  public int GetCount () {return datas.size (); 
  Public Object getitem (int position) {return datas.get (position); 
  public long getitemid (int position) {return position; 
    Public View GetView (final int position, View Convertview, ViewGroup parent) {Viewholder holder = null; 
      if (Convertview = = null) {holder = new Viewholder (); 
  Convertview = inflater.inflate (R.layout.list_item, NULL);    holder.coating = (TextView) Convertview.findviewbyid (r.id.tv_coating); 
      Holder.functions = (TextView) Convertview.findviewbyid (r.id.tv_functions); 
      holder.list_item_textview= (TextView) Convertview.findviewbyid (R.id.list_item_textview); 
    Convertview.settag (holder); 
    else {holder = (Viewholder) convertview.gettag (); 
 
    } holder.list_item_textview.setText (Datas.get (position)); 
     
    Holder.coating.setVisibility (view.visible); 
     
    Holder.functions.setClickable (FALSE); 
  return convertview; 
    Public final class Viewholder {public TextView coating; 
    public TextView functions; 
  Public TextView List_item_textview; 
 } 
}

Finally, we'll write the code inside the Xlistviewactivity, as shown in the following code:

Java code

Import java.util.ArrayList; 
Import Me.maxwin.view.XListView; 
Import Me.maxwin.view.XListView.IXListViewListener; 
Import Me.maxwin.view.XListView.RemoveListener; 
Import android.app.Activity; 
Import Android.content.Context; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
 
Import Android.widget.AdapterView.OnItemClickListener; public class Xlistviewactivity extends activity implements Ixlistviewlistener, removelistener,onitemclicklistener{pri 
Vate Xlistview Mlistview; 
  Private arrayadapter<string> Madapter; 
Private Itemadapter adapter; 
  private context; 
  Private arraylist<string> items = new arraylist<string> (); 
  Private Handler Mhandler; 
  private int start = 0; 
  private static int refreshcnt = 0; /** called the activity is a. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreatE (savedinstancestate); 
    Setcontentview (R.layout.main); 
    Geneitems (); 
     
    Init (); 
  Mhandler = new Handler (); } private void Init () {//TODO auto-generated method Stub mlistview = (Xlistview) Findviewbyid (r.id.xlis 
    Tview); 
    Mlistview.setpullloadenable (TRUE); 
    Mlistview.setremovelistener (this); 
Mlistview.setonitemclicklistener (this); 
Mlistview.setpullloadenable (FALSE); 
    Mlistview.setpullrefreshenable (FALSE); 
    Mlistview.setxlistviewlistener (this); 
    Adapter=new Itemadapter (this); 
    Adapter.setdata (items); 
 
  Mlistview.setadapter (adapter);  
    private void Geneitems () {for (int i = 0; I!= ++i) {items.add ("Refresh cnt" + (++start)); 
    }} private void OnLoad () {Mlistview.stoprefresh (); 
    Mlistview.stoploadmore (); 
  Mlistview.setrefreshtime ("just"); 
     @Override public void Onrefresh () {mhandler.postdelayed (new Runnable () {@Override public void Run () {start = ++refreshcnt; 
        Items.clear (); 
        Geneitems (); 
        Madapter.notifydatasetchanged (); 
        Adapter=new Itemadapter (xlistviewactivity.this); 
        Adapter.setdata (items); 
        Mlistview.setadapter (adapter); 
      OnLoad (); 
  }, 2000); @Override public void Onloadmore () {mhandler.postdelayed (new Runnable () {@Override public V 
        OID Run () {geneitems (); 
        Adapter.notifydatasetchanged (); 
      OnLoad (); 
  }, 2000); @Override public void RemoveItem (int position) {//TODO auto-generated method stub mlistview.isslid 
    E = false; 
    MListView.itemView.findViewById (r.id.tv_coating). setvisibility (view.visible); 
    Items.remove (position); 
     
  Adapter.notifydatasetchanged (); 
    @Override public void Onitemclick (adapterview<?> parent, view view, int position, long ID) { TODO auto-generatedMethod stub Intent Intent = new Intent (Getapplicationcontext (), itemactivity.class); 
    Intent.putextra ("Item", Items.get (position)); 
    StartActivity (Intent); 
     
  Overridependingtransition (R.anim.slide_in_from_right, r.anim.remain_original_location); 
 } 
 
}

      At this point, the above is the Android implementation   Drop-down refresh and load and sliding delete knowledge collation, thank you for your support!           

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.