Android 中的 ListView 项下方动态添加按钮
Dynamically Add buttons below ListView items in Android
我正在尝试在单击项目时在 ListView 项目下方动态创建 button
。
下面是我的 ListView 在项目被点击前后的例子
之前:
项目 1
项目 2
项目 3
点击项目 2 后:
项目 1
项目 2
[Text] [Text] [Button]
项目 3
如何在点击的列表项下方动态添加一行文本项和按钮?
为您的列表项制作自定义布局。在那边设置一个线性布局,设置为不可见状态。然后您可以通过使其可见来添加视图。在您的适配器中调用 setnotifydatachanged。
检查这个 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
)
);
最好用ExpandableListView
。当您单击 parent 项目时,然后在 parent 下方展开 child 视图显示。您可以在 child 上添加任何视图。
并且您可以检查以下内容link以实现扩展视图。
我正在尝试在单击项目时在 ListView 项目下方动态创建 button
。
下面是我的 ListView 在项目被点击前后的例子
之前:
项目 1
项目 2
项目 3
点击项目 2 后:
项目 1
项目 2
[Text] [Text] [Button]
项目 3
如何在点击的列表项下方动态添加一行文本项和按钮?
为您的列表项制作自定义布局。在那边设置一个线性布局,设置为不可见状态。然后您可以通过使其可见来添加视图。在您的适配器中调用 setnotifydatachanged。
检查这个 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
)
);
最好用ExpandableListView
。当您单击 parent 项目时,然后在 parent 下方展开 child 视图显示。您可以在 child 上添加任何视图。
并且您可以检查以下内容link以实现扩展视图。