2015-10-31 57 views
1

我试图在单击该项目时动态创建一个低于ListView项目的button在Android中的ListView项目下动态添加按钮

下面是之前的项目被点击

之前之后我的ListView的例子:


项目1


项目2


项目3


后项目2点:


项目1


项目2

[Text] [Text] [Button]


项目3


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

+0

这些按钮在list_view_row.xml中处于不可见状态,一旦你点击物品,调用setnotifydataset改变并且在你的适配器的getView()方法中改变按钮的状态为visible。 – dex

+0

如何向我们展示您的努力? – GottZ

回答

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 
     ) 
    ); 
+0

其他选项可用。无需为小用途使用库! – Killer

+0

只是建议,他有选择! – Mohamed

1

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

2

这是更好地使用ExpandableListView。当您单击父项时,则在父项的下方花费一个子视图显示。你可以添加任何视图到孩子。

而且您可以检查以下link以实施消耗性视图。