Android GridView添加头部问题的解决
我们都知道ListView有addHeaderView和addFooterView两个方法。其中addHeaderView是添加头部布局,addFooterView是添加底部布局。但是GridView却没有这两个方法这个时候就需要重写GridView了。geogle官方给出了重写的HeaderGridView不知道为什么没有添加到官方api里面。代码如下:
publicclassHeaderGridViewextendsGridView{ privatestaticfinalStringTAG="HeaderGridView"; /** *Aclassthatrepresentsafixedviewinalist,forexampleaheaderatthetop *orafooteratthebottom. */ privatestaticclassFixedViewInfo{ /**Theviewtoaddtothegrid*/ publicViewview; publicViewGroupviewContainer; /**Thedatabackingtheview.Thisisreturnedfrom{@linkListAdapter#getItem(int)}.*/ publicObjectdata; /**true
ifthefixedviewshouldbeselectableinthegrid*/ publicbooleanisSelectable; } privateArrayListmHeaderViewInfos=newArrayList (); privatevoidinitHeaderGridView(){ super.setClipChildren(false); } publicHeaderGridView(Contextcontext){ super(context); initHeaderGridView(); } publicHeaderGridView(Contextcontext,AttributeSetattrs){ super(context,attrs); initHeaderGridView(); } publicHeaderGridView(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); initHeaderGridView(); } @SuppressLint("NewApi") @Override protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ super.onMeasure(widthMeasureSpec,heightMeasureSpec); ListAdapteradapter=getAdapter(); if(adapter!=null&&adapterinstanceofHeaderViewGridAdapter){ ((HeaderViewGridAdapter)adapter).setNumColumns(getNumColumns()); } } @Override publicvoidsetClipChildren(booleanclipChildren){ //Ignore,sincetheheaderrowsdependonnotbeingclipped } /** *Addafixedviewtoappearatthetopofthegrid.IfaddHeaderViewis *calledmorethanonce,theviewswillappearintheordertheywere *added.Viewsaddedusingthiscallcantakefocusiftheywant. * *NOTE:CallthisbeforecallingsetAdapter.ThisissoHeaderGridViewcanwrap *thesuppliedcursorwithonethatwillalsoaccountforheaderviews. * *@paramvTheviewtoadd. *@paramdataDatatoassociatewiththisview *@paramisSelectablewhethertheitemisselectable */ publicvoidaddHeaderView(Viewv,Objectdata,booleanisSelectable){ ListAdapteradapter=getAdapter(); if(adapter!=null&&!(adapterinstanceofHeaderViewGridAdapter)){ thrownewIllegalStateException( "Cannotaddheaderviewtogrid--setAdapterhasalreadybeencalled."); } FixedViewInfoinfo=newFixedViewInfo(); FrameLayoutfl=newFullWidthFixedViewLayout(getContext()); fl.addView(v); info.view=v; info.viewContainer=fl; info.data=data; info.isSelectable=isSelectable; mHeaderViewInfos.add(info); //inthecaseofre-addingaheaderview,oraddingonelateron, //weneedtonotifytheobserver if(adapter!=null){ ((HeaderViewGridAdapter)adapter).notifyDataSetChanged(); } } /** *Addafixedviewtoappearatthetopofthegrid.IfaddHeaderViewis *calledmorethanonce,theviewswillappearintheordertheywere *added.Viewsaddedusingthiscallcantakefocusiftheywant. *
*NOTE:CallthisbeforecallingsetAdapter.ThisissoHeaderGridViewcanwrap *thesuppliedcursorwithonethatwillalsoaccountforheaderviews. * *@paramvTheviewtoadd. */ publicvoidaddHeaderView(Viewv){ addHeaderView(v,null,true); } publicintgetHeaderViewCount(){ returnmHeaderViewInfos.size(); } /** *Removesapreviously-addedheaderview. * *@paramvTheviewtoremove *@returntrueiftheviewwasremoved,falseiftheviewwasnotaheader *view */ publicbooleanremoveHeaderView(Viewv){ if(mHeaderViewInfos.size()>0){ booleanresult=false; ListAdapteradapter=getAdapter(); if(adapter!=null&&((HeaderViewGridAdapter)adapter).removeHeader(v)){ result=true; } removeFixedViewInfo(v,mHeaderViewInfos); returnresult; } returnfalse; } privatevoidremoveFixedViewInfo(Viewv,ArrayList
where){ intlen=where.size(); for(inti=0;i 0){ HeaderViewGridAdapterhadapter=newHeaderViewGridAdapter(mHeaderViewInfos,adapter); intnumColumns=getNumColumns(); if(numColumns>1){ hadapter.setNumColumns(numColumns); } super.setAdapter(hadapter); }else{ super.setAdapter(adapter); } } privateclassFullWidthFixedViewLayoutextendsFrameLayout{ publicFullWidthFixedViewLayout(Contextcontext){ super(context); } @Override protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ inttargetWidth=HeaderGridView.this.getMeasuredWidth() -HeaderGridView.this.getPaddingLeft() -HeaderGridView.this.getPaddingRight(); widthMeasureSpec=MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.getMode(widthMeasureSpec)); super.onMeasure(widthMeasureSpec,heightMeasureSpec); } } /** *ListAdapterusedwhenaHeaderGridViewhasheaderviews.ThisListAdapter *wrapsanotheroneandalsokeepstrackoftheheaderviewsandtheir *associateddataobjects. * Thisisintendedasabaseclass;youwillprobablynotneedto *usethisclassdirectlyinyourowncode. */ privatestaticclassHeaderViewGridAdapterimplementsWrapperListAdapter,Filterable{ //Thisisusedtonotifythecontainerofupdatesrelatingtonumberofcolumns //orheaderschanging,whichchangesthenumberofplaceholdersneeded privatefinalDataSetObservablemDataSetObservable=newDataSetObservable(); privatefinalListAdaptermAdapter; privateintmNumColumns=1; //ThisArrayListisassumedtoNOTbenull. ArrayList
mHeaderViewInfos; booleanmAreAllFixedViewsSelectable; privatefinalbooleanmIsFilterable; publicHeaderViewGridAdapter(ArrayList headerViewInfos,ListAdapteradapter){ mAdapter=adapter; mIsFilterable=adapterinstanceofFilterable; if(headerViewInfos==null){ thrownewIllegalArgumentException("headerViewInfoscannotbenull"); } mHeaderViewInfos=headerViewInfos; mAreAllFixedViewsSelectable=areAllListInfosSelectable(mHeaderViewInfos); } publicintgetHeadersCount(){ returnmHeaderViewInfos.size(); } @Override publicbooleanisEmpty(){ return(mAdapter==null||mAdapter.isEmpty())&&getHeadersCount()==0; } publicvoidsetNumColumns(intnumColumns){ if(numColumns<1){ thrownewIllegalArgumentException("Numberofcolumnsmustbe1ormore"); } if(mNumColumns!=numColumns){ mNumColumns=numColumns; notifyDataSetChanged(); } } privatebooleanareAllListInfosSelectable(ArrayList infos){ if(infos!=null){ for(FixedViewInfoinfo:infos){ if(!info.isSelectable){ returnfalse; } } } returntrue; } publicbooleanremoveHeader(Viewv){ for(inti=0;i =numHeadersAndPlaceholders){ intadjPosition=position-numHeadersAndPlaceholders; intadapterCount=mAdapter.getCount(); if(adjPosition =numHeadersAndPlaceholders){ intadjPosition=position-numHeadersAndPlaceholders; intadapterCount=mAdapter.getCount(); if(adjPosition 用法和ListView的addHeaderView一样。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。