java - Listview with pull to refersh Scroll down to last item when adding header-view -


i have created 1 pull refresh listview when scroll or pull little bit have add header view.

here custom listview class

package com.app.refreshablelist; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.drawable.drawable; import android.os.handler; import android.os.message; import android.util.attributeset; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewconfiguration; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import android.widget.progressbar; import android.widget.textview;  import com.app.xxxxxx.r; import com.google.android.gms.internal.el;  public class refreshablelistview extends listview {      boolean isscrool = false;     private view mheadercontainer = null;     private view mheaderview = null;     private imageview marrow = null;     private progressbar mprogress = null;     private textview mtext = null;     private float = 0;     private float mhistoricaly = 0;     private int mhistoricaltop = 0;     private int minitialheight = 0;     private boolean mflag = false;     private boolean marrowup = false;     private boolean misrefreshing = false;     private int mheaderheight = 0;     private onrefreshlistener mlistener = null;      private static final int refresh = 0;     private static final int normal = 1;     private static final int header_height_dp = 62;     private static final string tag = refreshablelistview.class.getsimplename();      private listviewobserver mobserver;     private view mtrackedchild;     private int mtrackedchildprevposition;     private int mtrackedchildprevtop;     ontouchlistener touch;      view vheader;      @override     protected void onscrollchanged(int l, int t, int oldl, int oldt) {         super.onscrollchanged(l, t, oldl, oldt);         if (mtrackedchild == null) {             if (getchildcount() > 0) {                 mtrackedchild = getchildinthemiddle();                 mtrackedchildprevtop = mtrackedchild.gettop();                 mtrackedchildprevposition = getpositionforview(mtrackedchild);             }         } else {             boolean childissafetotrack = mtrackedchild.getparent() ==                     && getpositionforview(mtrackedchild) == mtrackedchildprevposition;             if (childissafetotrack) {                 int top = mtrackedchild.gettop();                 if (mobserver != null) {                     float deltay = top - mtrackedchildprevtop;                     mobserver.onscroll(deltay);                 }                 mtrackedchildprevtop = top;             } else {                 mtrackedchild = null;             }         }      }      private view getchildinthemiddle() {         return getchildat(getchildcount() / 2);     }      public void setobserver(listviewobserver observer) {         mobserver = observer;     }      public refreshablelistview(final context context) {         super(context);         initialize();     }      public refreshablelistview(final context context, final attributeset attrs) {         super(context, attrs);         initialize();     }      public refreshablelistview(final context context, final attributeset attrs,             final int defstyle) {         super(context, attrs, defstyle);         initialize();     }      public void setonrefreshlistener(final onrefreshlistener l) {         mlistener = l;     }      @override     public void setontouchlistener(ontouchlistener l) {         // todo auto-generated method stub         super.setontouchlistener(l);     }      public void completerefreshing() {         mprogress.setvisibility(view.invisible);         marrow.setvisibility(view.visible);         mhandler.sendmessage(mhandler.obtainmessage(normal, mheaderheight, 0));         misrefreshing = false;         invalidateviews();     }      @override     public boolean onintercepttouchevent(final motionevent ev) {         switch (ev.getaction()) {         case motionevent.action_down:             mhandler.removemessages(refresh);             mhandler.removemessages(normal);             = mhistoricaly = ev.gety();             if (mheadercontainer.getlayoutparams() != null) {                 minitialheight = mheadercontainer.getlayoutparams().height;             }             break;         }         return super.onintercepttouchevent(ev);     }      @override     public boolean ontouchevent(final motionevent ev) {         switch (ev.getaction()) {         case motionevent.action_move:             mhistoricaltop = getchildat(0).gettop();             break;         case motionevent.action_up:             if (!misrefreshing) {                 if (marrowup) {                     startrefreshing();                     mhandler.sendmessage(mhandler.obtainmessage(refresh,                             (int) (ev.gety() - my) / 2 + minitialheight, 0));                 } else {                     if (getchildat(0).gettop() == 0) {                         mhandler.sendmessage(mhandler.obtainmessage(normal,                                 (int) (ev.gety() - my) / 2 + minitialheight, 0));                     }                 }             } else {                 mhandler.sendmessage(mhandler.obtainmessage(refresh,                         (int) (ev.gety() - my) / 2 + minitialheight, 0));             }             mflag = false;             break;         }         return super.ontouchevent(ev);     }      @override     public boolean dispatchtouchevent(final motionevent ev) {         if (ev.getaction() == motionevent.action_move                  && getfirstvisibleposition() == 0) {             float direction = ev.gety() - mhistoricaly;             int height = (int) (ev.gety() - my) / 2 + minitialheight;             if (height < 0) {                 height = 0;             }              float deltay = math.abs(my - ev.gety());             viewconfiguration config = viewconfiguration.get(getcontext());             if (deltay > config.getscaledtouchslop()) {                  // scrolling downward                 if (direction > 0) {                     // refresh bar extended if top pixel of first item                     // visible                     if (getchildat(0) != null) {                         if (getchildat(0).gettop() == 0) {                             if (mhistoricaltop < 0) {                                  // = ev.gety(); // todo works without                                 // this?mhistoricaltop = 0;                             }                             // if (isscrool == true) {                             //                             // } else {                             // isscrool = true;                             // addheaderview(vheader);                             //                             // // animation anim = animationutils.loadanimation(                             // // getcontext(), r.anim.bounce_animation);                             // // startanimation(anim);                             //                             // smoothscrolltoposition(getchildat(0).gettop());                             // // bottom_layout.setvisibility(view.visible);                             // }                             // extends refresh bar                              /*****                              * commented me on 10-09-2014                              */                             setheaderheight(height);                              // stop list scroll prevent list                             // overscrolling                             ev.setaction(motionevent.action_cancel);                             mflag = false;                         }                     }                  } else if (direction < 0) {                     // scrolling upward                      // refresh bar shortened if top pixel of first item                     //                     // visible                     if (getchildat(0) != null) {                         if (getchildat(0).gettop() == 0) {                             setheaderheight(height);                              // if scroll reaches top of list, list scroll                             // enabled                             if (getchildat(1) != null                                     && getchildat(1).gettop() <= 1 && !mflag) {                                 ev.setaction(motionevent.action_down);                                 mflag = true;                             }                         }                     }                 }             }              mhistoricaly = ev.gety();         }         try {             return super.dispatchtouchevent(ev);         } catch (exception e) {             return false;         }     }      @override     public boolean performitemclick(final view view, final int position,             final long id) {         if (position == 0) {             // refresh header element             return true;         } else {             return super.performitemclick(view, position - 1, id);         }     }      private void initialize() {          layoutinflater inflator = (layoutinflater) getcontext()                 .getsystemservice(context.layout_inflater_service);         vheader = inflator.inflate(r.layout.search_header, null);         layoutinflater inflater = (layoutinflater) getcontext()                 .getsystemservice(context.layout_inflater_service);         mheadercontainer = inflater.inflate(r.layout.refreshable_list_header,                 null);         mheaderview = mheadercontainer                 .findviewbyid(r.id.refreshable_list_header);         marrow = (imageview) mheadercontainer                 .findviewbyid(r.id.refreshable_list_arrow);         mprogress = (progressbar) mheadercontainer                 .findviewbyid(r.id.refreshable_list_progress);         mtext = (textview) mheadercontainer                 .findviewbyid(r.id.refreshable_list_text);         addheaderview(mheadercontainer);          mheaderheight = (int) (header_height_dp * getcontext().getresources()                 .getdisplaymetrics().density);          setheaderheight(0);     }      private void setheaderheight(final int height) {         if (height <= 1) {             mheaderview.setvisibility(view.gone);         } else {             mheaderview.setvisibility(view.visible);         }          // extends refresh bar         layoutparams lp = (layoutparams) mheadercontainer.getlayoutparams();         if (lp == null) {             lp = new layoutparams(layoutparams.fill_parent,                     layoutparams.wrap_content);         }         lp.height = height;         mheadercontainer.setlayoutparams(lp);          // refresh bar shows bottom top         linearlayout.layoutparams headerlp = (linearlayout.layoutparams) mheaderview                 .getlayoutparams();         if (headerlp == null) {             headerlp = new linearlayout.layoutparams(layoutparams.fill_parent,                     layoutparams.wrap_content);         }          headerlp.topmargin = -mheaderheight + height;          mheaderview.setlayoutparams(headerlp);          if (!misrefreshing) {             // if scroll reaches trigger line, start refreshing             if (height > mheaderheight && !marrowup) {                 marrow.startanimation(animationutils.loadanimation(                         getcontext(), r.anim.rotate));                 mtext.settext("release update");                 rotatearrow();                 marrowup = true;             } else if (height < mheaderheight && marrowup) {                 marrow.startanimation(animationutils.loadanimation(                         getcontext(), r.anim.rotate));                 mtext.settext("pull down update");                 rotatearrow();                 marrowup = false;             }else {                 if (isscrool == true) {                  } else {                      isscrool = true;                     addheaderview(vheader);                      // animation anim = animationutils.loadanimation(                     // getcontext(), r.anim.bounce_animation);                     // startanimation(anim);                      smoothscrolltoposition(getchildat(0).gettop());                     // bottom_layout.setvisibility(view.visible);                 }             }         }     }      private void rotatearrow() {         drawable drawable = marrow.getdrawable();         bitmap bitmap = bitmap.createbitmap(drawable.getintrinsicwidth(),                 drawable.getintrinsicheight(), config.argb_8888);         canvas canvas = new canvas(bitmap);         canvas.save();         canvas.rotate(180.0f, canvas.getwidth() / 2.0f,                 canvas.getheight() / 2.0f);         drawable.setbounds(0, 0, drawable.getintrinsicwidth(),                 drawable.getintrinsicheight());         drawable.draw(canvas);         canvas.restore();         marrow.setimagebitmap(bitmap);     }      private void startrefreshing() {         marrow.setvisibility(view.invisible);         mprogress.setvisibility(view.visible);         mtext.settext("loading...");         misrefreshing = true;          if (mlistener != null) {             mlistener.onrefresh(this);         }     }      private final handler mhandler = new handler() {          @override         public void handlemessage(final message msg) {             super.handlemessage(msg);              int limit = 0;             switch (msg.what) {             case refresh:                 limit = mheaderheight;                 break;             case normal:                 limit = 0;                 break;            }              // elastic scrolling             if (msg.arg1 >= limit) {                 setheaderheight(msg.arg1);                 int displacement = (msg.arg1 - limit) / 10;                 if (displacement == 0) {                     mhandler.sendmessage(mhandler.obtainmessage(msg.what,                             msg.arg1 - 1, 0));                 } else {                     mhandler.sendmessage(mhandler.obtainmessage(msg.what,                             msg.arg1 - displacement, 0));                 }             }         }      };      public interface onrefreshlistener {         public void onrefresh(refreshablelistview listview);     }      public static interface listviewobserver {         public void onscroll(float deltay);     } } 

here in method dispatchtouchevent(final motionevent ev) have make 1 condition

if (isscrool == true) {  } else {     isscrool = true;     addheaderview(vheader);      // animation anim =     // animationutils.loadanimation(     // getcontext(), r.anim.bounce_animation);     // startanimation(anim);      smoothscrolltoposition(getchildat(0).gettop());     // bottom_layout.setvisibility(view.visible); } 

here addheaderview(vheader); adds header listview scroll down last item can tell me what's happening here?

try call listview.setselectionafterheaderview();. scroll listview top.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -