Android 自定义ListView,解决嵌套ListView的显示不完全的问题

本篇博客和上篇博客自定义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>

本篇博客在这里就结束了,有不足的地方还望各路大神指导 ,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值