[英]Android ExpandableListView Parent with Button
I am trying to achieve something like this. 我正在努力实现这样的目标。 The Expandable List consists of the names of certain categories and when a parent is clicked, it shows the list of all the children in that category. 可扩展列表由某些类别的名称组成,单击父级时,它会显示该类别中所有子级的列表。 Now, suppose I want dynamically add a child to any category ? 现在,假设我想动态地将孩子添加到任何类别? How do I do that ? 我怎么做 ? Do I keep a button with every parent in the list clicking on which would add a new child under it ? 我是否按下列表中的每个父母点击一个按钮,在其下面添加一个新的孩子?
But looking around in different forums, I came to realize that it is not really easy to set a button click handler inside every parent. 但是在不同的论坛中环顾四周,我开始意识到在每个父级内部设置一个按钮点击处理程序并不容易。 But if that is the only way, can anyone give me some sample code please ? 但如果这是唯一的方法,有人可以给我一些示例代码吗?
I found this thread but wasn't able to implement it in my code. 我找到了这个帖子但是无法在我的代码中实现它。 Android Row becomes Unclickable with Button 使用Button,Android Row变为Unclickable
Adding a button to the group view shouldn't be that difficult. 向组视图添加按钮不应该那么困难。
I believe the below should work (although I don't have a project using an array backed ExpandableListView to test on). 我相信下面应该工作(虽然我没有一个项目使用数组支持ExpandableListView来测试)。
I don't know your group row layout, so I'll make one up here for reference purposes. 我不知道你的团队行布局,所以我会在这里做一个以供参考。
group_layout.xml 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>
Then in your getGroupView
method from your adapter: 然后在你的适配器的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.