Android GridView Add Headview

Source: Internet
Author: User

Package com.example.test;import android.content.context;import android.util.attributeset;import  android.util.log;import android.view.motionevent;import android.view.viewconfiguration;import  android.widget.gridview;import android.widget.linearlayout;public class headgridview  extends LinearLayout{private static final String TAG =  HeadGridView.class.getSimpleName ();p rivate linearlayout mheadlayout;private gridview  mgridview;private int mheadheight;private boolean misbeingdragged;private float  mlastmotiony;private float mtouchslop;private boolean mgridviewfocused = true; Private state mstate = state.getdefaultmode ();p Ublic headgridview (Context  Context)  {this (context, null);} Public headgridview (context context, attributeset attrs)  {super (context, attrs); InchItcontentview (context);} Private void initcontentview (Context context)  {ViewConfiguration config =  Viewconfiguration.get (context); Mtouchslop = config.getscaledtouchslop (); setorientation (VERTICAL); Gridview gridview = new gridview (context); Layoutparams gridparams = new layoutparams (layoutparams.fill_parent,  layoutparams.fill_parent); Gridview.setnumcolumns (2); Gridview.setlayoutparams (gridparams); mGridView =  gridview;initgridview (GridView); AddView (GridView); Linearlayout headlayout = new linearlayout (context); Layoutparams headparams = new layoutparams (layoutparams.fill_parent,  layoutparams.wrap_content); Headlayout.setlayoutparams (headparams); Mheadlayout = headlayout;inithead ( Headlayout); AddView (headlayout,0);} @Overrideprotected  void onmeasure (Int widthmeasurespec, int heightmeasurespec)  { Super.onmeasure (widthmEASURESPEC,&NBSP;HEIGHTMEASURESPEC); int parentwidth = measurespec.getsize (widthMeasureSpec); int  parentheight = measurespec.getsize (Heightmeasurespec);int gridwidthspec =  Measurespec.makemeasurespec (parentwidth, measurespec.exactly);int gridheightspec =  Measurespec.makemeasurespec (parentheight, measurespec.exactly); Mgridview.measure (gridWidthSpec,  GRIDHEIGHTSPEC); setmeasureddimension (parentwidth, parentheight);} @Overrideprotected  void onlayout (boolean changed, int l, int t, int &NBSP;R,&NBSP;INT&NBSP;B)  {super.onlayout (changed, l, t, r, b); mHeadHeight =  getchildat (0). Getmeasuredheight ();} Protected void initgridview (Gridview gridview)  {}protected void inithead ( linearlayout headlayout)  {} @Overridepublic  final boolean onintercepttouchevent ( motionevent event) &NBSP;{LOG.D (tag, " currentstate =  "+mstate+"  event =  "+event.getaction () +"  misbeingdragged =   "+misbeingdragged"; Final int action = event.getaction ();if  (action ==  motionevent.action_cancel | | &NBSP;ACTION&NBSP;==&NBSP;MOTIONEVENT.ACTION_UP)  {misbeingdragged = false;return false;} if  (misbeingdragged)  {return true;} switch  (Action)  {case motionevent.action_move:final float y = event.gety (); Final float absdiff = math.abs (Y - mlastmotiony); if  (Absdiff > mtouchslop)  {mLastMotionY = y;mIsBeingDragged = true; if  (mstate != state.head_visible)  {mgridviewfocused = false;}} Break;case motionevent.action_down:mlastmotiony = event.gety ();mIsBeingDragged =  False;break;default:break;} return misbeingdragged;} @Overridepublic  boolean Ontouchevent (motionevent event)  {if  (event.getaction ()  == motionevent.action_down  && event.getedgeflags ()  != 0)  {return false;} switch  (Event.getaction ())  {case MotionEvent.ACTION_MOVE: {if  (misbeingdragged)   {pullevent (event); return true;} break;} Case motionevent.action_down: mlastmotiony = event.gety ();return true;case  motionevent.action_cancel:case motionevent.action_up: if  (misbeingdragged)  { Misbeingdragged = false;} Return true;} Return false;} Private void dispatchdowntogridview (Motionevent e, float downy)  {int action  = e.getaction (); Motionevent de = motionevent.obtain (e);d e.setlocation (De.getx (),  downy);d e.setaction ( Motionevent.action_down); mgridview.dispatchtouchevent (DE); E.setaction (ACTION);} Private void pullevent (motionevent e) &NBSP;{IF&NBSP; (!mgridviewfocused)  {dispatchdowntogridview (e, mlastmotiony); mgridviewfocused = true;} Float y = e.gety (); Int scrollvalue = math.round (mlastmotiony - y); if   (mstate == state.bottom_top)  {if  (scrollvalue > 0) {if  (mstate  == state.bottom_top)  {dispatchdowntogridview (e, mlastmotiony);} Mgridview.dispatchtouchevent (e); mstate = state.bottom_medium;}  else {mstate = state.head_visible;}} if  (Mstate.canheadviewscroll ())  {int currentscrolly = getscrolly ();int  maxscrooly = mheadheight - currentscrolly;if  (scrollvalue > 0)  {  //slide up if  (maxscrooly < scrollvalue)  {scrollvalue = maxscrooly;mstate  = state.bottom_top;} Scrollby (0, scrollvalue);}  else { //slide down if  (currentscrolly > 0)  {if  (cUrrentscrolly < - scrollvalue)  {scrollvalue = - currentscrolly;} Scrollby (0, scrollvalue);}}}  else {mgridview.dispatchtouchevent (e);if  (scrollvalue < 0)  {int top  = mgridview.getchildat (0). GetTop ();if  (top == 0)  {mState =  State.bottom_top;}}} Mlastmotiony = y;} Public static enum state{head_visible (0x01), Bottom_top (0x02), Bottom_bottom (0x03), Bottom_Medium ( 0x04);p rivate int id;private state (Int id)  {this.id = id;} Public int getid () {return id;} Public static state getdefaultmode () {return state.head_visible;} Static state mapstate (Final int id)  {for  (state value :  State.values ())  {if  (Id == value.getid ())  {return value;} Return getdefaultmode ();} Boolean canheadviewscroll ()  {return this  == head_visible | |  this == bottom_top;}}}

Simple implementation, there are still some problems, follow-up can be consulted: https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android /photos/views/headergridview.java

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.