AndroidScrollView与ListView,GridView共存冲突解决方案我们在真实项目中通常会遇到ListView或者GridView嵌套在ScrollView中问题。但是做的时候会发现,一旦两者进行嵌套,即会发生冲突。得不到我们希望的效果。由于ListView和GridView本身都继承于ScrollView,一旦在ScrollView中嵌套ScrollView,那么里面的ScrollView高度计算就会出现问题。我们也就无法得到想要的效果。下面进入正题,我们将分别讨论ScrollView中嵌套ListView和FGridView的情况:核心解决方案:重写ListView或者GridView的OnMesure方法:[java]@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){//TODOAuto-generatedmethodstubintexpandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec,expandSpec);}一、ScrollView中嵌套ListViewBlogScrollViewActivity.java代码:[java]packagecom.csdn.blog.scrollview;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.ArrayAdapter;importandroid.widget.BaseAdapter;importandroid.widget.GridView;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.ListView;importandroid.widget.ScrollView;importandroid.widget.LinearLayout.LayoutParams;importandroid.widget.TextView;publicclassBlogScrollViewActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*///MyGridViewgrid;ImageViewimage;ScrollViewscroll;String[]texts=newString[]{无线,通话设置,声音,显示,位置,应用,账户,隐私权,存储,语言,游戏,娱乐,电影,音乐,辅助功能,日期};/*ArrayAdapterStringadapter;*/TestListViewlist;LinearLayout.LayoutParamslp;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}voidinit(){list=(TestListView)findViewById(R.id.list);image=(ImageView)findViewById(R.id.image);list.setAdapter(newGridAdapter(this));scroll=(ScrollView)findViewById(R.id.scroll);scroll.requestChildFocus(image,null);}privateclassGridAdapterextendsBaseAdapter{Activitycontext;publicGridAdapter(Activitycontext){this.context=context;}@OverridepublicintgetCount(){//TODOAuto-generatedmethodstubreturntexts.length;}@OverridepublicObjectgetItem(intposition){//TODOAuto-generatedmethodstubreturnnull;}@OverridepubliclonggetItemId(intposition){//TODOAuto-generatedmethodstubreturn0;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){//TODOAuto-generatedmethodstubViewHolderholder=null;if(convertView==null){convertView=context.getLayoutInflater().inflate(R.layout.item,null);holder=newViewHolder();holder.text=(TextView)convertView.findViewById(R.id.grid_text);convertView.setTag(holder);}else{holder=(ViewHolder)convertView.getTag();}holder.text.setText(texts[position]);returnconvertView;}classViewHolder{TextViewtext;}}}TestListView.java代码如下:[java]packagecom.csdn.blog.scrollview;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.View.MeasureSpec;importandroid.widget.ListView;publicclassTestListViewextendsListView{publicTestListView(Contextcontext){super(context);//TODOAuto-generatedconstructorstub}publicTestListView(Contextcontext,AttributeSetattrs){super(context,attrs);//TODOAuto-generatedconstructorstub}publicTestListView(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);//TODOAuto-generatedconstructorstub}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){//TODOAuto-generatedmethodstubintexpandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec,expandSpec);}}main.xml代码:[html]p?xmlversion=1.0encoding=utf-8?LinearLayoutxmlns:android=ahref=://schemas.android.com/apk/res/android/aandroid:layout_width=fill_parentandroid:layout_height=fill_parentandroid:orientation=verticalandroid:background=#FFFFFFScrollViewandroid:layout_height=fill_parentandroid:layout_width=fill_parentandroid:fadingEdgeLength=0dpandroid:scrollbars=noneandroid:id=@+id/scrollLinearLayoutandroid:layout_height=fill_parentandroid:layout_width=fill_parentandroid:orientation=verticalImageViewandroid:id=@+id/imageandroid:layout_height=150dpandroid:layout_width=fill_parentandroid:padding=2dpandroid:scaleType=centerCropandroid:src=@drawable/fruit/pp/com.csdn.blog.scrollview.TestListViewandroid:id=@+id/listandroid:layout_height=fill_parentandroid:layout_width=fill_parentandroid:fadingEdgeLength=0dpandroid:scrollbars=none//LinearLayout/ScrollView/pp/LinearLayout/p效果图如下:这里我的布局方式是上面一张图片,下面放置listView。对于此种布局方式,可以通过另外一种方式避免此问题。由于ListView有addHeadView()方法,那么我们可以直接将上面想加入的View通过getLayoutInflater().inflate(this,R.layout.***)加入到ListView的顶部即可。二、ScrollView中嵌套GridView的解决方案。ScrollView中嵌套GridView,最简单的方法就是重写GridView方法,使其在绘制时重新计算GridView高度MyGridView.java代码如下:[java]packagecom.csdn.blog.scrollview;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.widget.GridView;publicclassMyGridViewextendsGridView{publicMyGridView(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);//TODOAuto-generatedconstructorstub}publicMyGridView(Contextcontext,AttributeSetattrs){super(context,attrs);//TODOAuto-generatedconstructorstub}publicMyGridView(Contextcontext){super(context);//TOD