gpt4 book ai didi

带按钮的 Android ExpandableListView 父级

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:54 25 4
gpt4 key购买 nike

我正在努力实现这样的目标。可扩展列表由某些类别的名称组成,单击父项时,它会显示该类别中所有子项的列表。现在,假设我想动态地将一个 child 添加到任何类别?我怎么做 ?我是否为列表中的每个 parent 保留一个按钮,单击该按钮会在其下方添加一个新 child ?

但是环顾不同的论坛,我开始意识到在每个父级中设置按钮单击处理程序并不是一件容易的事。但如果这是唯一的方法,谁能给我一些示例代码?

我找到了这个线程,但无法在我的代码中实现它。 Android Row becomes Unclickable with Button

最佳答案

将按钮添加到组 View 应该不会那么困难。

我相信下面的应该可行(尽管我没有使用数组支持的 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;
}

关于带按钮的 Android ExpandableListView 父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11015230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com