在ScrollView嵌套ListView

一开始折腾了半天检查了所有的代码,还是不能显示所有的列表,无语了,最后看到xml里面有这么一条

警告:The vertically scrolling ScrollView should not contain another vertically scrolling widget (ListView)

百度一下,竟然解决了,又涨姿势了,好高兴,现记录如下。

布局部分代码如下:

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="50.0dip" >

            <com.farsunset.ichat.mydiy.MyListView
                android:id="@+id/setting_listview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/logoutButton"
                android:layout_width="fill_parent"
                android:layout_height="50.0dip"
                android:layout_marginLeft="10.0dip"
                android:layout_marginRight="10.0dip"
                android:layout_marginTop="30.0dip"
                android:background="@drawable/button_blue"
                android:gravity="center"
                android:scaleType="centerInside"
                android:text="@string/label_setting_logout"
                android:textColor="@color/white"
                android:textSize="14.0sp" />
        </LinearLayout>
    </ScrollView>

如果不用自定义的ListView的话就会出现那条警告,导致listView无法正常显示。

自定义的ListView代码如下:

package com.farsunset.ichat.mydiy;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class MyListView extends ListView {

    public MyListView(Context paramContext) {
        super(paramContext);
    }

    public MyListView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }

    public MyListView(Context paramContext, AttributeSet paramAttributeSet,
            int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

记住这个onMeasure方法必须得重载。

这样就ok了,感谢这篇文章:http://blog.csdn.net/gaojinshan/article/details/17055511

相关推荐:

如何在 Python 中安全更新字典中嵌套值(以图书可借状态为例)

本文详解如何正确更新嵌套字典中的值,重点解决因键不存在导致的keyerror问题,并提供健壮、可复用的字典更新函数实现。 本文详解如何正确更新嵌套字典中的值,重点解决因键不存在导致的keyerror问题,并提供健壮、可复用的字典更新函数实现。 在Python中更新字典内嵌套结构的值(例如catalo...

如何递归重命名嵌套字典中的键名(基于映射字典)

本文介绍一种通用、健壮的递归方法,用于根据指定的键映射字典(key_dict)批量重命名嵌套字典中任意层级的键名,支持多级嵌套映射,避免浅层匹配错误与空字典返回问题。 本文介绍一种通用、健壮的递归方法,用于根据指定的键映射字典(key_dict)批量重命名嵌套字典中任意层级的键名,支持多级嵌套映射,...

如何在嵌套字典中按层级关系精准递归提取元素值

本文提供一种健壮的递归方法,用于在任意深度的嵌套字典中,依据「主键(mainkey)→子键(subkey)→目标元素(element)」的逻辑路径查找值,支持非连续嵌套结构,避免传统线性遍历的漏匹配问题。 本文提供一种健壮的递归方法,用于在任意深度的嵌套字典中,依据「主键(mainkey)→子键(s...