ScrollView是否不支持添加多个视图?

3
如果是这样的话,那么ScrollView就相当无能了(难以置信),或者有其他的方法可以解决。以下是我的代码。在循环第二次执行时会出错(也就是注释掉的地方)。
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize);

    ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost);

    // Contacts data snippet adapted from
    // http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Create a Linear Layout for each contact?
            LinearLayout llay = new LinearLayout(this);
            llay.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams llp = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llp.weight = 1.0f;

            CheckBox cbOnDemand = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbOnDemand.setLayoutParams(llp);
            llay.addView(cbOnDemand);

            CheckBox cbTime = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbTime.setLayoutParams(llp);
            llay.addView(cbTime);

            CheckBox cbSpace = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbSpace.setLayoutParams(llp);
            llay.addView(cbSpace);

            TextView tv = new TextView(getApplicationContext());
            tv.setTag(id);
            tv.setText(name);
            tv.setLayoutParams(llp);
            llay.addView(tv);

            svh.addView(llay); // it transports me to Eclipse's Debug perspective when I hit this line the SECOND time around.
            // One cat on stackOverflow said to do this, another said it
            // would be unnecessary
            svh.invalidate();
        }
    }
}
3个回答

11

您可以采取以下两种方法来解决这个问题。

1) 使 ScrollView 的唯一子元素成为一个垂直的 LinearLayout,并将所有子元素添加到 LinearLayout 中,而不是添加到 ScrollView 中。

2) 更好的选择是使用一个 ListView(可能是在一个 ScrollView 内部实现了一个 LinearLayout)。


谢谢你的帮助。我会选择选项A,因为选项2存在问题(这是我第一次尝试解决这个问题),我无法找到在运行时以编程方式识别每个复选框的方法 - 如何知道单击哪个行上的哪个复选框。我需要复选框具有与联系人ID相对应的标记值,以便我知道为每个联系人选择了哪些选项。这就是为什么我选择动态创建小部件路线,这样我可以在创建复选框(和textView)小部件的循环中添加该标记。 - B. Clay Shannon-B. Crow Raven

7
ScrollView 只能包含一个视图。但是,这个视图可以是一个布局,比如 LinearLayout。通常我的做法是将这样的视图添加到 ScrollView 中,这样就很好用了。
例子:
<ScrollView>
  <LinearLayout>
    <ImageView/>
    <TextView/>
  </LinearLayout>
</ScrollView>

1
以防他仍然感到困惑。LinearLayout 直接放置在 ScrollView 中。它只是用来包装你想放进去的所有东西。 - w.donahue
1
我认为我正在将LinearLayout放入ScrollView中;我猜我需要随后创建包含三个复选框和textView的LinearLayout,并将它们添加到最外层的LinearLayout,而不是ScrollView。 - B. Clay Shannon-B. Crow Raven
九年过去了,这仍然是正确的。 - Joe

1

嵌套滚动视图只能拥有一个直接子视图,如果您想添加更多,则必须将一个视图作为视图组的子视图包含进去,例如线性布局,它可以容纳更多的视图,因此您可以在其他视图中添加按钮等。换句话说,如果您将它们作为NestedScrollView的子视图放置,则视图组可以容纳多个视图。我将一个布局作为NestedScrollView的子视图,并且它还有许多其他视图。

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/descriptioncontent_layout" />

</android.support.v4.widget.NestedScrollView>

添加多个项目到描述内容布局...等等。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="5dp">

    <include layout="@layout/descption_layout" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="5dp">

    <include layout="@layout/howtodoit" />
</android.support.v7.widget.CardView>

Description_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Full Body:"
    android:id="@+id/description"
    android:textSize="20sp"
    android:textColor="@color/how"
    android:padding="10dp"
    android:layout_marginLeft="10dp"
    android:layout_gravity="center|left" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="If you are brand new to yoga, there are certain postures that are essential for you to learn so you can feel comfortable in a class or practicing on your own at home"
    android:id="@+id/detaildescription"
    android:textSize="18sp"
    android:padding="10dp"
    android:layout_gravity="center_horizontal" />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接