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是可以滑动的就是显示不全
用自己写的方法之后才显示出来了所有的条目
那就不再废话了 把我个人研究的代码呈上
首先是关于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
猜你喜欢
- 本文实例讲述了Java使用组合模式实现表示公司组织结构功能。分享给大家供大家参考,具体如下:一、模式定义组合模式:将对象组合成树形结构以表示
- 在本文中,我们将通过用C#重构一个非常简单的代码示例来解释依赖注入和IoC容器。 简介:依赖注入和IoC乍一看可能相当复杂,但它们
- 前言随着网络技术的发展、计算机应用水平广泛提高,原来系统的时效性、数据的正确性、操作的方便性上都存在不足,已影响到系统的正常使用。经过考察比
- SpringAOP获取方法参数上的注解一、示例① 如下代码,自定义一个参数注解@Test,并将其使用到方法参数上,用于标注需要检验的参数/*
- 一、什么是IOC1)控制反转,把创建对象和对象的调用过程交给Spring 管理。2)使用IOC的目的,为了降低耦合度。二、IOC的底层原理X
- 前言在看一本关于高性能编程的时候发现 Java8 中关于接口的新特性的介绍,这个特性是真的棒,解决了一个接口中有多个方法,但并不想实现该接口
- 今天提交代码的时候突然发现IDEA的SVN面板Version Control下面多出来这么一句话:顿时心里咯噔一下,这肯定不是好事啊。果然,
- 本文实例为大家分享了java制作简单验证码的具体代码,供大家参考,具体内容如下在这里我们需要用到java的画笔工具,所以我们需要导入以下包i
- 本文实例讲述了C#面向对象编程之猜拳游戏实现方法。分享给大家供大家参考。具体实现方法如下:1.需求现在要制作一个游戏,玩家与计算机进行猜拳游
- 双重循环打印顶点在左上的直角三角形:public static void main(String[] args) { // TO
- JDK * 实现原理 * 机制通过实现 InvocationHandler 接口创建自己的调用处理器通过为 Proxy 类指定 Clas
- MyBatis-Plus 使用简单,内置通用 Mapper、通用 Service,仅仅通过少量配置,即可实现单表大部分 CRUD 操作。下面
- init_output_stream() 是一个公共的函数,无论是音频,还是视频的输出流的初始化,都是通过它来完成的。init_o
- 在Java中,我们可以对List集合进行如下几种方式的遍历:List<Integer> list = new ArrayList
- 1.前言string是属于引用类型的,这个大家都知道吧?但是平常在使用的过程中,发现它还是拥有一些值类型的特征的,这到底是为什么呢?原因就是
- MyBatis介绍MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software
- 本文实例讲述了Android实现EditText中添加和删除bitmap的方法。分享给大家供大家参考,具体如下:SpannableStrin
- 确保这个修改是正确的(否则将会出现乱码)创建i18n文件夹(就是国际化的意思),然后在此文件加下创login.properties logi
- AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。其实AsyncTask并不是那么好,甚至有些糟糕。本文
- spring拓展 定义自己的namespace1.查看源码认识spring是怎么加载xml配置的1.1 spring是怎么创建对象的? 查看