Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和ListView都是不能完全显示,显示的基本上都是单行的数据,最后查找资料和翻阅文档看到原因是ListView和GridView的绘制过程中在ScrollView中无法准确的测量自身的高度,而且listVIew和GridView抢占了焦点,使得ListView和GrideView具有自身的显示的效果,这样就测量出显示一行条目即可的距离,其他的条目根据自身的滑动显示。

我的XMl的部分代码如下:

 <ScrollView    andROId:layout_height="match_parent"   android:layout_width="fill_parent"   android:scrollbars="none"   >   <LinearLayout     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="#23000000"     android:orientation="vertical"     android:paddingTop="-1dp" >     <android.support.v4.view.ViewPager       android:id="@+id/home_pager"       android:layout_width="fill_parent"       android:layout_height="100dp" >     </android.support.v4.view.ViewPager>     <GridView       android:id="@+id/gv_home"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:numColumns="5"       android:paddingBottom="10dp"       android:paddingTop="13dp" >     </GridView>     <LinearLayout       android:id="@+id/ll_clickin"       android:layout_width="fill_parent"       android:layout_height="30dp"       android:layout_marginBottom="10dp"       android:layout_marginLeft="10dp"       android:layout_marginRight="10dp"       android:background="@drawable/Shape_home"       android:orientation="horizontal" >       <ImageView         android:layout_width="85dp"         android:layout_height="wrap_content"         android:src="@drawable/click_in" />       <TextView         android:layout_width="match_parent"         android:layout_height="fill_parent"         android:gravITy="center"         android:singleLine="true"         android:text="限时抢购 ,快点进入吧!"         android:textSize="18sp" />     </LinearLayout>     <ListView       android:id="@+id/list_home"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:background="#ffffff" >     </ListView>   </LinearLayout> </ScrollView>

显示的效果是这样的其中的Listview和GridView是可以滑动的就是显示不全

Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

用自己写的方法之后才显示出来了所有的条目&nbsp;

Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

那就不再废话了 把我个人研究的代码呈上

首先是关于ListView的 (注意此方法必须方到SetAdapter()方法之后执行)

这是控件的查找

 list_home = (ListView) view.findViewById(R.id.list_home); list_home.setAdapter(new MyListViewAdapter(grideview_List)); 
  list_home.setFocusable(false);//词句加不加像也没有什么影响,我是加的  //setAdapter之后调用  getListViewSelfHeight(list_home);

这是getListViewSelfHeight(ListView youListview)

 public void getListViewSelfHeight(ListView listView) {         // 获取ListView对应的Adapter       ListAdapter listAdapter = listView.getAdapter();       //健壮性的判断     if (listAdapter == null) {         return;       }      // 统计所有子项的总高度       int totalHeight = 0;       for (int i = 0, len = listAdapter.getCount(); i < len; i++) {         // listAdapter.getCount()返回数据项的数目         View listItem = listAdapter.getView(i, null, listView);         // 调用Measure方法 传0是测量默认的大小       listItem.measure(0, 0);         totalHeight += listItem.getMeasuredHeight();       }       //通过父控件进行高度的申请     ViewGroup.LayoutParams params = listView.getLayoutParams();       //listAdapter.getCount() - 1 从零开始 listView.getDividerHeight()获取子项间分隔符占用的高度      params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));       listView.setLayoutParams(params);     }

下面是GridView的方法和ListView的测量的方法基本一样  但是listView是单行条目的不用在担心列的问题问GridView则是需要进行自己分行和自己分列的 所以要注意一下

         gv_home = (GridView) view.findViewById(R.id.gv_home);   gv_home.setSelector(new ColorDrawable(Color.TRANSPARENT));  home_pager.setFocusable(false);  gv_home.setAdapter(new MyGrigeViewAdapter(grideview_List));  getGridViewSelfHeight(gv_home);

下面是getGridViewSelfHeight(GridView youGrideView)(这个方法能解决问题但是感觉不是很好灵活性太差 我用的获取的列数始终获取不到,有看神看到了 给我回复)

 public void getGridViewSelfhetght(GridView gridView) {       // 获取GridView对应的Adapter       ListAdapter adapter = gridView.getAdapter();       if (adapter == null) {         return;       }       int totalHeight = 0;       for (int i = 0, len = adapter.getCount(); i < len; i++) {         // gridView.getCount()返回数据项的数目         View listItem = adapter.getView(i, null, gridView);         // 计算子项View 的高         listItem.measure(0, 0);         //此处方法并不好       //5其中5是我们在XML中的android:numColumns="5"       //FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3)设置下边据       totalHeight += listItem.getMeasuredHeight()/5+FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3);       }       ViewGroup.LayoutParams params = gridView.getLayoutParams();      params.height = totalHeight;       gridView.setLayoutParams(params);     }

下面是FontDisplayUtil类

 import android.content.Context; /**  * dp、sp 、 px之间的相互转化的工具类  */ public class FontDisplayUtil {  /**  * 将px值转换为dip或dp值,保证尺寸大小不变  */  public static int px2dip(Context context, float pxValue) {  final float scale = context.getResources().getDisplayMetrics().density;  return (int) (pxValue / scale + 0.5f);  }  /**  * 将dip或dp值转换为px值,保证尺寸大小不变  */  public static int dip2px(Context context, float dipvalue) {  final float scale = context.getResources().getDisplayMetrics().density;  return (int) (dipValue * scale + 0.5f);  }  /**  * 将px值转换为sp值,保证文字大小不变  */  public static int px2sp(Context context, float pxValue) {  final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  return (int) (pxValue / fontScale + 0.5f);  }  /**  * 将sp值转换为px值,保证文字大小不变  */  public static int sp2px(Context context, float spValue) {  final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  return (int) (spValue * fontScale + 0.5f);  } }

总结

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题全部内容,希望文章能够帮你解决Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。