我试图在单击项目时在ListView项下动态创建button

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

之前:



项目1



项目2



项目3



单击项目2后:



项目1



项目2

[Text] [Text] [Button]



项目3



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

最佳答案

选中此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
        )
    );

关于android - 在Android中的ListView项下方动态添加按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33448808/

10-10 07:42