我正在努力实现这样的目标。 可扩展列表由某些类别的名称组成,单击父级时,它会显示该类别中所有子级的列表。 现在,假设我想动态地将孩子添加到任何类别? 我怎么做 ? 我是否按下列表中的每个父母点击一个按钮,在其下面添加一个新的孩子?
但是在不同的论坛中,我开始意识到在每个父级中设置按钮点击处理程序并不容易。但如果这是唯一的方法,那么有人可以给我一些示例代码吗?
我找到了这个帖子但是无法在我的代码中实现它。 Android Row becomes Unclickable with Button
答案 0 :(得分:6)
向组视图添加按钮不应该那么困难。
我相信以下内容应该可行(尽管我没有使用支持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;
}