The recent project intends to do an interface, similar to the DayOne home page interface effect, DayOne is a paid application, currently only iOS side. As a senior lazy programmer, the aim is to never repeat the creation of a wheel. So, go to the Internet to find a lot of open source projects, found no suitable, and then, can only bite the bullet himself. First look at the effect:
Effect chart
In fact, writing is also relatively simple, is to control the head and the bottom of the height of the ListView can be, if the recycleview to achieve the same, just recycleview add head and tail slightly more trouble, processing Click events are not very convenient, So it is based on the ListView to achieve. Implementation of the code that I have uploaded to the github.
1, the use of methods
Compile ' com.a520wcf.yllistview:yllistview:1.0.1
2, the use of the introduction:
1), Layout:
Layout note A small detail android:layout_height best is match_parent, otherwise listview each time when sliding may need to recalculate the entry height, the comparison consumes CPU;
<com.a520wcf.yllistview.yllistview
android:divider= "@android: color/transparent"
android:id= "@+id/ ListView "
android:layout_width=" match_parent "
android:layout_height=" match_parent "/>"
2), Code:
Private Yllistview ListView;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
ListView = (Yllistview) Findviewbyid (R.id.listview);
Do not add also have default head and bottom
View topview=view.inflate (this,r.layout.top,null);
Listview.addheaderview (TopView);
View Bottomview=new View (Getapplicationcontext ());
Listview.addfooterview (Bottomview);
The top and bottom can also be fixed to the final height of the listview.setfinalbottomheight to use the height of the layout itself
(MB);
Listview.setfinaltopheight (m);
Listview.setadapter (New Demoadapter ());
Yllistview default header and bottom handle Click event position note minus
Listview.setonitemclicklistener (new Adapterview.onitemclicklistener () {
@Override public
void Onitemclick (adapterview<?> parent, view view, int position, long id) {
Position=position-listview.getheaderviewscount ();}}
);
3, Source introduction
In fact, there is only one class in this project, we do not need to rely on, directly to copy this class to the project can be, to see the source code:
Package Com.a520wcf.yllistview;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewTreeObserver.OnGlobalLayoutListener;
Import Android.view.animation.DecelerateInterpolator;
Import Android.widget.AbsListView;
Import Android.widget.ListView;
Import Android.widget.Scroller; public class Yllistview extends ListView implements Abslistview.onscrolllistener {private scroller mscroller;//Used FO
R Scroll back private float mlasty =-1;
private int mscrollback;
Private final static int scrollback_header = 0;
Private final static int scrollback_footer = 1; Private final static int scroll_duration = 400;
Scroll back duration private final static float Offset_radio = 1.8f;
Total list items, used to detect are at the bottom of ListView.
private int mtotalitemcount; Private View Mheaderview; Top picture private View Mfooterview;
Bottom picture private int finaltopheight; private int FinalBottomheight;
Public Yllistview {Super (context);
Initwithcontext (context);
Public Yllistview (context, AttributeSet attrs) {Super (context, attrs);
Initwithcontext (context);
Public Yllistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Initwithcontext (context);
} private void Initwithcontext {Mscroller = new Scroller (context, new Decelerateinterpolator ());
Super.setonscrolllistener (this); This.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Override public void O
Ngloballayout () {if (mheaderview==null) {view View=new view (GetContext ());
Addheaderview (view);
} if (mfooterview==null) {view View=new view (GetContext ());
Addfooterview (view);
} getviewtreeobserver (). Removeglobalonlayoutlistener (this);
}
}); @Override public boolean ontouchevent (motioNevent ev) {if (mlasty = = 1) {mlasty = Ev.getrawy ();
Switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:mLastY = Ev.getrawy ();
Break
Case MotionEvent.ACTION_MOVE:final Float DeltaY = Ev.getrawy ()-mlasty;
Mlasty = Ev.getrawy (); if (getfirstvisibleposition () = = 0 && (mheaderview.getheight () > Finaltopheight | | deltay > 0) &&A mp
Mheaderview.gettop () >= 0) {//The is showing, header has shown or pull down.
Updateheaderheight (Deltay/offset_radio); else if (getlastvisibleposition () = = MTotalItemCount-1 && (getfootheight () >finalbottomheight | | delt
AY < 0)) {updatefooterheight (-deltay/offset_radio);
} break; Default:mlasty =-1; Reset if (getfirstvisibleposition () = = 0 && getheaderheight () > Finaltopheight) {resetheaderheight
(); } if (getlastvisibleposition () = = mTotalItemCount-1) {if Getfootheight () &Gt
Finalbottomheight) {resetfooterheight ();
}} break;
return super.ontouchevent (EV);
/** * Reset Bottom Height * * private void Resetfooterheight () {int bottomheight = Getfootheight ();
if (Bottomheight > Finalbottomheight) {mscrollback = Scrollback_footer;
Mscroller.startscroll (0, bottomheight, 0,-bottomheight+finalbottomheight, scroll_duration);
Invalidate ();
The system automatically calls @Override public void Computescroll () {if (Mscroller.computescrolloffset ()) {invalidate ()) {
if (Mscrollback = = Scrollback_header) {setheaderheight (Mscroller.getcurry ());
else {setfooterviewheight (Mscroller.getcurry ());
} postinvalidate ();
} super.computescroll (); //set top height private void setheaderheight (int height) {layoutparams layoutparams = (layoutparams) mheaderview.getlayou
Tparams ();
Layoutparams.height = height;
Mheaderview.setlayoutparams (Layoutparams); }//Set bottom height private void setfooterviewheight (int height) {Layoutparams layoutparams = (layoutparams) mfooterview.getlayoutparams ();
Layoutparams.height =height;
Mfooterview.setlayoutparams (Layoutparams); //Get top height public int getheaderheight () {Abslistview.layoutparams layoutparams = (abslistview.layoutparams) mHea
Derview.getlayoutparams ();
return layoutparams.height; //Get bottom height public int getfootheight () {Abslistview.layoutparams layoutparams = (abslistview.layoutparams) mfoote
Rview.getlayoutparams ();
return layoutparams.height;
private void Resetheaderheight () {int height = getheaderheight ();
if (height = = 0)//not visible.
Return
Mscrollback = Scrollback_header;
Mscroller.startscroll (0, height, 0, finaltopheight-height, scroll_duration);
Invalidate (); /** * Set the top height if you do not set the height, the default is the height of the layout itself * @param height at the top of the height */public void setfinaltopheight (int height) {This.fin
Altopheight = height; /** * Set the bottom height if you do not set the height, the default is the height of the layout itself * @param height at the bottom level * * Public VOID setfinalbottomheight (int height) {this.finalbottomheight=height;
@Override public void Addheaderview (View v) {mheaderview = v;
Super.addheaderview (Mheaderview); Mheaderview.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Override public
void Ongloballayout () {if (finaltopheight==0) {finaltopheight = Mheaderview.getmeasuredheight ();
} setheaderheight (Finaltopheight);
Getviewtreeobserver (). Removeglobalonlayoutlistener (this);
}
});
@Override public void Addfooterview (View v) {mfooterview = v;
Super.addfooterview (Mfooterview); Mfooterview.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Override public
void Ongloballayout () {if (finalbottomheight==0) {finalbottomheight = Mfooterview.getmeasuredheight ();
} setfooterviewheight (Finalbottomheight); Getviewtreeobserver (). RemoveglobalOnlayoutlistener (this);
}
}); Private Onscrolllistener Mscrolllistener;
User ' s scroll listener @Override public void Setonscrolllistener (Onscrolllistener l) {mscrolllistener = l; @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {if (Mscrolllistener!= null) {m
Scrolllistener.onscrollstatechanged (view, scrollstate); @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemc
Ount) {//Send to user ' s listener mtotalitemcount = Totalitemcount;
if (Mscrolllistener!= null) {Mscrolllistener.onscroll (view, Firstvisibleitem, VisibleItemCount, Totalitemcount);
} private void Updateheaderheight (float delta) {setheaderheight (int) (Getheaderheight () +delta)); SetSelection (0); Scroll to top each time} private void Updatefooterheight (float delta) {setfooterviewheight (int) (Getfootheight (
) (+delta));
}
}
The above is the entire content of this article, I hope to help you learn.