android - android中linearlayout中scrollview listview如何解决

标签 android android-listview scrollview android-linearlayout

这里我在我的线性布局中使用 ScrollView 。但是我想在我的 ScrollView 线性布局中包含另一个 ListView 。

Scrollview 只支持一个子项。我知道在 scrollview 里面 listview 不工作。但是有没有其他选项可以使用 listview。我想滚动所有内容。

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

最佳答案

ScrollView 中的 ListView 肯定无法正常工作。

我能想到的唯一方法是在界面中只有一个 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;
    }
}

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

关于android - android中linearlayout中scrollview listview如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10943526/

相关文章:

android - Facebook 时间线,如 ListView

c# - 没有为 ListView 启用 ScrollViewer

java - 添加 View 后无法刷新布局(仅发生在 CustomAdapter 中)

SDK 20 升级后,Eclipse (ADT) 的 Android 插件消失

android - 使用 React Native 访问 Android 上的相机和 CameraRoll?

java - Android ListView Adapter 错误调用notifyDataSetChanged,Android bug?

android - 具有重复的基本适配器列表项的自定义适配器

android - 动态创建的 ListView 项目的 View 持有者模式

Android AlarmClock ACTION_SET_ALARM Intent 产生异常

iOS 8 - UIScrollView 不滚动