繁体   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