1

这里我在我的线性布局中使用滚动视图。但我想在我的滚动视图线性布局中包含另一个列表视图。

滚动视图只支持一个孩子。我知道滚动视图内部的列表视图不起作用。但是有没有其他选项可以使用列表视图。我想滚动所有内容。

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        android:id="@+id/widget54"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
        <LinearLayout
            android:layout_width="310px"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button 1"
                />
            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button 2"
                />
            <Button
                android:id="@+id/button3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button 3"
                />
            <EditText
                android:id="@+id/txt"
                android:layout_width="fill_parent"
                android:layout_height="300px"
                />
            <Button
                android:id="@+id/button4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button 4"
                />

        </LinearLayout>
    </ScrollView>
4

2 回答 2

1

ScrollView 中的 ListView 很肯定不起作用。

我能想到的唯一方法是,界面中只有一个列表视图,并为它创建一个自定义适配器:

public class HeaderAdapter{
    private Adapter mAdapter; // this adapter is the adapter that you will normally have, for your listview

    getViewTypeCount(){
        return mAdapter.getViewTypeCount() + 1;
    }

    getView(int pos, View convertView){
        // Handle only the first position, othervise, let mAdapter return
        if (pos!=0) return mAdapter.getView(pos-1, convertView);

        // 
        View v = LayoutInflater.inflate(R.layout.other_linear_layout);
        // Bind the v if necessary

        return v;
    }
}

以上只是一个示例,想法是您将其余内容(线性布局、按钮、edittext 等)视为适配器的“标题”行。

于 2012-06-08T05:40:24.197 回答
0

如果 ScrollView 中只有一个 ListView,则可以在布局 XML 中为 ScrollView 添加一个属性: android:fillViewport="true"

这将展开 ListView 的所有内容。

否则,如果ScrollView中有多个ListView或GridView,则需要创建一个customListView。

扩展 ListView 和 @Override onMeasure:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
}
于 2016-03-06T09:21:20.933 回答