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

作者:ChaoLi_Chen 时间:2023-09-26 23:32:58 

最近为公司做的一个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中内容显示不全的问题

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

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);
}
}

来源:https://blog.csdn.net/ChaoLi_Chen/article/details/51320558

标签:scrollview,gridview,listview,显示不全
0
投稿

猜你喜欢

  • C#使用foreach遍历哈希表(hashtable)的方法

    2022-10-07 20:00:44
  • Unity3D应用之时钟与钟表小组件的使用教程

    2023-05-15 04:43:35
  • Android编程实现为ListView创建上下文菜单(ContextMenu)的方法

    2021-12-09 06:26:28
  • java 对称加密算法实现详解

    2021-07-29 22:34:57
  • Android编程实现动态支持多语言的方法

    2021-11-01 12:38:25
  • Java Redis Redisson配置教程详解

    2022-10-13 06:32:39
  • Android实现文件解压带进度条功能

    2023-02-26 19:01:05
  • Flutter web bridge 通信总结分析详解

    2022-05-23 05:15:42
  • Java List的remove()方法踩坑

    2021-05-27 05:17:58
  • JavaFX Metro UI 和 开发库使用简介

    2021-12-11 19:01:39
  • Java中的BaseTypeHandler自定义类型转换器的使用

    2022-03-09 00:34:16
  • unity 如何获取button文本的内容

    2022-01-29 19:31:04
  • android中Bitmap用法(显示,保存,缩放,旋转)实例分析

    2022-07-03 14:18:41
  • Android使用Gallery实现照片拖动的特效

    2022-12-31 23:04:22
  • Java编写迷宫小游戏

    2021-08-06 12:53:33
  • Android中文件读写(输入流和输出流)操作小结

    2023-07-23 02:53:19
  • Java如何跳出当前多重循环你知道吗

    2022-12-17 02:22:17
  • Android服务Service教程

    2022-02-19 13:05:40
  • Android网易有道词典案例源码分享

    2022-09-18 20:53:25
  • 深入解析Java中的Classloader的运行机制

    2023-07-16 11:47:59
  • asp之家 软件编程 m.aspxhome.com