簡體   English   中英

帶有Button的Android ExpandableListView Parent

[英]Android ExpandableListView Parent with Button

我正在努力實現這樣的目標。 可擴展列表由某些類別的名稱組成,單擊父級時,它會顯示該類別中所有子級的列表。 現在,假設我想動態地將孩子添加到任何類別? 我怎么做 ? 我是否按下列表中的每個父母點擊一個按鈕,在其下面添加一個新的孩子?

但是在不同的論壇中環顧四周,我開始意識到在每個父級內部設置一個按鈕點擊處理程序並不容易。 但如果這是唯一的方法,有人可以給我一些示例代碼嗎?

我找到了這個帖子但是無法在我的代碼中實現它。 使用Button,Android Row變為Unclickable

向組視圖添加按鈕不應該那么困難。

我相信下面應該工作(雖然我沒有一個項目使用數組支持ExpandableListView來測試)。

我不知道你的團隊行布局,所以我會在這里做一個以供參考。

group_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/addbutton"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Add"
        android:textSize="12dp" />
</LinearLayout>

然后在你的適配器的getGroupView方法中:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    if (convertView == null) {
        View convertView = View.inflate(getApplicationContext(), R.layout.group_layout, null);
        Button addButton = (Button)convertView.findViewById(R.id.addButton);

        addButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // your code to add to the child list
            }
        });
    }        
    TextView textView = (TextView)convertView.findViewById(R.id.text1);
    textView.setText(getGroup(groupPosition).toString()); 
    return convertView; 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM