【问题标题】:Android ExpandableListView Parent with Button带有按钮的 Android ExpandableListView 父级
【发布时间】:2012-06-13 12:51:10
【问题描述】:
我正在努力实现这样的目标。
可扩展列表由某些类别的名称组成,当单击父级时,它会显示该类别中所有子级的列表。
现在,假设我想动态地将孩子添加到任何类别?
我怎么做 ?
我是否在列表中的每个父母都保留一个按钮,单击该按钮会在其下添加一个新孩子?
但是在不同的论坛中环顾四周,我开始意识到在每个父级中设置按钮单击处理程序并不容易。但如果这是唯一的方法,谁能给我一些示例代码吗?
我找到了这个线程,但无法在我的代码中实现它。
Android Row becomes Unclickable with Button
【问题讨论】:
标签:
android
button
expandablelistview
【解决方案1】:
将按钮添加到组视图应该没有那么困难。
我相信以下应该可以工作(尽管我没有使用数组支持的 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;
}