Android Custom ListView Layout implements pull-load drop-down refresh function _android

Source: Internet
Author: User
Tags diff gety xmlns

There are a number of ways that ListView can implement pull loads and drop-down refreshes. The following is a custom layout I wrote, and the reusability is also relatively strong. The first is the inherited ListView custom View.

Autolistview.java:

Package Com.example.mic.testdemo.view; 
Import Android.annotation.TargetApi; 
Import Android.content.Context; 
Import Android.os.Build; 
Import Android.os.Bundle; 
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.Animation; 
Import Android.view.animation.LinearInterpolator; 
Import android.view.animation.RotateAnimation; 
Import Android.widget.AbsListView; 
Import Android.widget.ImageView; 
Import Android.widget.ListView; 
Import Android.widget.ProgressBar; 
Import Android.widget.TextView; 
Import COM.EXAMPLE.MIC.TESTDEMO.R; 
Import Java.text.SimpleDateFormat; 
 /** * Created by Dflenovo on 2016/8/3. */public class Autolistview extends ListView implements Abslistview.onscrolllistener {public static final String FOO 
  TER = "FOOTER"; 
  public static final String Header = "header"; private static final int Pull = 1; 
  private static final int NONE = 0; 
  private static final int release = 2; 
  private static final int refreshing = 2; 
  private static final int space = 20; 
  private static final String TAG = "Autolistview"; 
  Private Rotateanimation downanimation; 
  Private Rotateanimation upanimation; 
  private context; 
  Private Layoutinflater Inflater; 
  Private View footer; 
  Private View header; 
  private int headercontentinitialheight; 
  private int headercontentheight; 
  Private Onrefreshlistener Onrefreshlistener; 
  Private Boolean loadenable; 
  Private Onloadlistener Onloadlistener; 
  private int firstvisibleitem; 
  Private Boolean isloadingmore = false; 
  private int starty; 
  private int state; 
  Private ImageView Iv_pull; 
  private int footerviewheight; 
  Private ProgressBar Mprogressbar; 
  Private TextView tvstate; 
  Private TextView Tvlastupdatetime; 
  Private Boolean Isscrolltobottom; Public Autolistview {Super (context); 
  Initview (context); 
    Public Autolistview (context, AttributeSet attrs) {Super (context, attrs); 
  Initview (context); Autolistview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr) 
    ; 
  Initview (context); } @TargetApi (Build.version_codes. Lollipop) public Autolistview (context, AttributeSet attrs, int defstyleattr, int defstyleres) {SUPER (cont 
    Ext, Attrs, defstyleattr, defstyleres); 
  Initview (context); 
    private void Initview (context context) {Initheaderview (); 
    Initfooterview (); 
This.setonscrolllistener (this); private void Initheaderview () {header = View.inflate (GetContext (), r.layout.pull_to_refresh_header, NULL) 
    ; 
    Iv_pull = (ImageView) header. Findviewbyid (R.id.iv_pull); 
    Mprogressbar = (ProgressBar) header. Findviewbyid (R.id.pb_listview_header); Tvstate = (TextView) header. findviewbYid (r.id.tv_listview_header_state); 
    Tvlastupdatetime = (TextView) header. Findviewbyid (R.id.tv_listview_header_last_update_time); 
    Set the last Refresh Time Tvlastupdatetime.settext ("Last Refresh Time:" + getlastupdatetime ()); Header.measure (0, 0); 
    The system will help us measure the height of the Headerview headercontentheight = Header.getmeasuredheight (); 
    Header.setpadding (0,-headercontentheight, 0, 0); This.addheaderview (header,header,true); 
  Add a View object Initanimation () to the top of the ListView;  } private void Initanimation () {upanimation = new rotateanimation (0f, -180f, Animation.relative_to_self, 
    0.5f, Animation.relative_to_self, 0.5f); 
    Upanimation.setduration (500); Upanimation.setfillafter (TRUE); After the animation is over, stay at the end of the position downanimation = new Rotateanimation ( -180f, -360f, Animation.relative_to_self, 0.5f, Anim ation. 
    Relative_to_self, 0.5f); 
    Downanimation.setduration (500); Downanimation.setfillafter (TRUE); After the animation ends, stay at the end of the position of the PrivAte String Getlastupdatetime () {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 
  Return Sdf.format (System.currenttimemillis ()); 
    private void Initfooterview () {footer = View.inflate (GetContext (), r.layout.foot_view, NULL); 
    Footer.measure (0, 0); 
    Footerviewheight = Footer.getmeasuredheight (); 
    Footer.setpadding (0,-footerviewheight, 0, 0); 
  This.addfooterview (footer,footer,true); 
    private void Measureview (View child) {Viewgroup.layoutparams P = child.getlayoutparams (); if (p = = null) {p = new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, Viewgroup.layoutpar Ams. 
    Wrap_content); 
    int childwidthspec = viewgroup.getchildmeasurespec (0, 0 + 0, p.width); 
    int lpheight = P.height; 
    int childheightspec; if (Lpheight > 0) {childheightspec = Measurespec.makemeasurespec (Lpheight, measurespec.exactly); 
    Is size. else {CHILDHEIGHTSPEC = Measurespec.makemeasurespec (0, measurespec.unspecified);//Gets the actual size of the child layout. 
  } child.measure (Childwidthspec, Childheightspec); } private void toppadding (int toppadding) {header.setpadding (Header.getpaddingleft (), toppadding, header 
    . Getpaddingright (), Header.getpaddingbottom ()); 
  Header.invalidate (); 
        @Override public void onscrollstatechanged (Abslistview abslistview, int i) {if (i = = Scroll_state_idle || i = = scroll_state_fling) {//Determine whether the current has been to the bottom if (isscrolltobottom &&!isloadingmore) {isload 
        Ingmore = true; 
        Current to bottom log.i (TAG, "load more Data"); 
        Footer.setpadding (0, 0, 0, 0); 
        This.setselection (This.getcount ()); 
        if (Onloadlistener!= null) {onloadlistener.onload (); @Override public void Onscroll (Abslistview abslistview, int i, int i1, int i2) {this.fi 
    Rstvisibleitem = i; if (getlastvisiblePosition () = = (i2-1)) {Isscrolltobottom = true; 
    else {isscrolltobottom = false; @Override public boolean ontouchevent (motionevent ev) {switch (ev.getaction ()) {//Case Motion 
        Event.ACTION_DOWN:startY = (int) ev.gety (); 
      Break 
        Case MotionEvent.ACTION_MOVE:int Movey = (int) ev.gety (); 
        Y in Move-y = spacing pressed. 
        int diff = (movey-starty)/2; 
        -Height + spacing of head layout = paddingtop int paddingtop =-headercontentheight + diff; 
        If:-Header layout height > Paddingtop value executes super.ontouchevent (EV);  if (Firstvisibleitem = = 0 &&-headercontentheight < paddingtop) {if (Paddingtop > 0 
            && state = NONE) {//fully displayed. 
            LOG.I (TAG, "release Refresh"); 
            state = PULL; 
          Refreshheaderviewbystate (); else if (Paddingtop < 0 && state = = PULL) {//No full log.i shown (TAG, "Drop-down refresh "); 
            state = NONE; 
          Refreshheaderviewbystate (); 
        }//Dropdown head layout header.setpadding (0, paddingtop, 0, 0); 
      } break; 
          Case MOTIONEVENT.ACTION_UP://Determine whether the current state is to release the refresh or Drop-down refresh if (s = = PULL) {log.i (TAG, "refresh data."); 
          The head layout is set to fully display the state header.setpadding (0, 0, 0, 0); 
          Go to the state = refreshing that is being refreshed; 
          Refreshheaderviewbystate ();  if (Onrefreshlistener!= null) {Onrefreshlistener.onrefresh ();//Calling the consumer's Listener method}} else if (state = NONE) 
        {//Hidden header layout header.setpadding (0,-headercontentheight, 0, 0); 
      } break; 
    Default:break; 
  return super.ontouchevent (EV); }//private void Refreshheaderviewbystate () {switch (state) {//Case NONE://dropdown refresh status Tvstate. 
        SetText ("Drop-down refresh"); Iv_pull.startanimation (DownanimatiON); 
      Perform a downward rotation break; 
        Case PULL://Release Refresh Status Tvstate.settext ("Release Refresh"); Iv_pull.startanimation (upanimation); 
      Perform a spin up break; 
        Case refreshing://is refreshing in the state iv_pull.clearanimation (); 
        Iv_pull.setvisibility (View.gone); 
        Mprogressbar.setvisibility (view.visible); 
        Tvstate.settext ("in refresh ..."); 
      Break 
    Default:break; 
    } public void Hideheaderview () {header.setpadding (0,-headercontentheight, 0, 0); 
    Iv_pull.setvisibility (view.visible); 
    Mprogressbar.setvisibility (View.gone); 
    Tvstate.settext ("Drop-down refresh"); 
    Tvlastupdatetime.settext ("Last Refresh Time:" + getlastupdatetime ()); 
  state = NONE; 
    public void Hidefooterview () {footer.setpadding (0,-footerviewheight, 0, 0); 
  Isloadingmore = false; } public void Setonrefreshlistener (Onrefreshlistener onrefreshlistener) {This.onrefreshlistener = OnRefreshListen 
  Er } public void SetonloadlisteneR (Onloadlistener onloadlistener) {this.onloadlistener = Onloadlistener; 
    public void Onrefresh () {if (Onrefreshlistener!= null) {Onrefreshlistener.onrefresh (); 
    } public void OnLoad () {if (Onloadlistener!= null) {onloadlistener.onload (); 
  } public interface Onrefreshlistener {//Definition dropdown flush interface public void Onrefresh (); 
  } public interface Onloadlistener {//Definition pull load more public void onLoad ();  } 
}

The code above is the key to implementation. The following is the header layout and the foot layout:

Foot_view.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= 
"http://schemas.android.com/apk" /res/android " 
  android:layout_width=" match_parent " 
  android:layout_height=" wrap_content "> 
  < TextView 
    android:layout_width= "wrap_content" 
    android:layout_height= "Wrap_content" 
    Load more " 
    android:textsize=" 20DP " 
    android:padding=" "10DP" 
    android:layout_centerhorizontal= "true"/> 

Pull_to_refresh_header.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <framelayout android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:l 
      ayout_margin= "10dip" > <imageview android:id= "@+id/iv_pull" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:minwidth= "30dip" a ndroid:src= "@mipmap/xlistview_arrow"/> <progressbar android:id= "@+id/pb_listview_header" Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" and roid:visibility= "Gone"/> </FrameLayout> <linearlayout android:layout_width= "Fill_parent" Andr oid:layout_height= "Wrap_content" 
    Android:layout_gravity= "center_vertical" android:gravity= "center_horizontal" android:orientation= "vertical" 
      "> <textview android:id=" @+id/tv_listview_header_state "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "Drop-down Refresh" android:textcolor= "#FF0000" android:texts Ize= "18sp"/> <textview android:id= "@+id/tv_listview_header_last_update_time" Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "5dip" android:text= "the most Refresh time: 2014-10-10 12:56:12 "android:textcolor=" @android: Color/black "android:textsize=" 14sp "/> </l  Inearlayout> </LinearLayout>

Activity_main.xml:

<?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:id=" @+id/activity_main 
  " 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.mic.testdemo.MainActivity" >       <com.example.mic.testdemo.view.autolistview 
        android:id= "@+id/lv" 
        android:layout_width= "match_parent" 
       android:layout_height= "Match_parent" > 
      </com.example.mic.testdemo.view.AutoListView> 

The step above is to load a different layout on the ListView, which is also the code to be reused, the following code is how to use the code.

Mainactivity.java:

public class Mainactivity extends appcompatactivity implements Autolistview.onloadlistener, Autolistview.onrefreshlistener { 
  private TextView TextView; 
  Private String result; 
  Private Autolistview ListView; 
  Private Myadater Adater; 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { 
    super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.activity_main); 
    ListView = (Autolistview) Findviewbyid (r.id.lv);} 
Listview.setonrefreshlistener (mainactivity.this); 
Listview.setonloadlistener (mainactivity.this); 
@Override public 
  void OnLoad () { 
  } 
  @Override 
  

Here is the implementation, where you need to set up listening, as well as the above load and refresh the required operation on it!

The above is a small set to introduce the Android custom ListView layout to achieve pull load drop-down refresh function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website 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.