在Android中动态添加ListView项目下方的按钮

时间:2015-10-31 06:35:57

标签: android dynamic android-listview

我想在单击项目时在ListView项目下面动态创建button

以下是单击项目之前和之后的ListView示例

之前:

的Item1

项目2

项目3

点击第2项后:

的Item1

项目2

[Text] [Text] [Button]

项目3

如何在点击的列表视图项下方动态添加一行文本项和按钮?

3 个答案:

答案 0 :(得分:2)

最好使用 $criteria = new CDbCriteria; $criteria->join = 'LEFT OUTER JOIN customer AS C ON C.id = t.customer_id AND C.store_id=' . Yii::app()->session['store_id']; $criteria->select = 'C.firstname as firstname'; $criteria->group = 'C.firstname'; $criteria->condition = 'store_id=\'' . Yii::app()->session['store_id'] . '\''; $list = CustomerLifecycle::model()->findAll($criteria); 。当您单击父项时,然后在父项下方展开子视图显示。您可以向儿童添加任何视图。

您可以检查以下link以实现消耗性视图。

答案 1 :(得分:1)

为列表项目制作自定义布局。在那里设置线性布局并设置为不可见状态。然后,您可以通过使其可见来添加视图。在你的适配器中调用setnotifydatachanged。

答案 2 :(得分:0)

选中此library,它会为您提供所需内容。

如图书馆文档中所述,将其添加到list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/text"
            android:text="Hello World"/>

    <!-- this is the button that will trigger sliding of the expandable view -->
    <Button
            android:id="@+id/expandable_toggle_button"
            android:text="More"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/text"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@id/text"/>

</RelativeLayout>

<!-- this is the expandable view that is initially hidden and will slide out when the more button is pressed -->
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/expandable"
        android:background="#000000">

    <!-- put whatever you want in the expandable view -->
    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action A" />

    <Button
            android:id="@+id/details"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action B"/>

    </LinearLayout>
</LinearLayout>

这是你的activity

ListView list = ... your list view
    ListAdapter adapter = ... your list adapter
    // now simply wrap the adapter
    // and indicate the ids of your toggle button
    // and expandable view
    list.setAdapter(new SlideExpandableListAdapter(
            adapter,
            R.id.expandable_toggle_button,
            R.id.expandable
        )
    );
相关问题