本篇博客和上篇博客自定义GridView,解决嵌套Grideview的显示不完全的问题原理是一样的,同样在这里也自定义一个ListView组件继承于原来的ListView,重写onMeasure方法重绘组件,然后替换布局文件中ListView布局即可。
package com.pumasoft.cakeboss.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* @author: xiaolijuan
* @description: 自定义ListView,解决嵌套ListView的显示不完全的问题
* @date: 2016-03-14
* @time: 10:50
*/
public class CustomListView extends ListView {
public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context) {
super(context);
}
/**
* 重寫onMeasure方法决定了组件显示的高度与宽度; makeMeasureSpec函数中第一个參數决定布局空间的大小,第二个参数是布局模式
* MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就扩展到多大的空间
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
现在只需要在布局文件中引入我们自定义的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.xiaolijuan.listViewdome.CustomListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" />
</LinearLayout>
本篇博客在这里就结束了,有不足的地方还望各路大神指导 ,谢谢!