【问题标题】:how to solve scrollview listview in linearlayout in android如何解决android中linearlayout中的scrollview listview
【发布时间】:2012-06-08 05:32:11
【问题描述】:

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

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

<?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>

【问题讨论】:

标签: android android-listview scrollview android-linearlayout


【解决方案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 等)视为适配器的“标题”行。

【讨论】:

    【解决方案2】:

    如果你的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多