gpt4 book ai didi

Android expandablelistview组/父背景图片在折叠或展开时发生变化

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:53 24 4
gpt4 key购买 nike

我尝试在折叠或展开时交换 expandablelistview 组背景图像。但是背景图像没有正确交换。例如,当我展开第一组时,第二组或第三组的背景会交换。

我正在使用 RelativeLayout(组布局)和 SimpleExpandableListAdapter(适配器)。这是我所做的:

// Create 2 drawable background. One for collapse and one for expand
private Drawable collapseBG, expandBG;
private ExpandableListView myView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
collapseBG = getResources().getDrawable(R.drawable.box_bg_header_collapse);
expandBG = getResources().getDrawable(R.drawable.box_bg_header_expand);
myView = (ExpandableListView) findViewById(R.id.myView);
}

myView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
public void onGroupCollapse(int groupPosition_c) {
myView.getChildAt(groupPosition_c).setBackgroundDrawable(collapseBG);
}
});

myView.setOnGroupExpandListener (new ExpandableListView.OnGroupExpandListener() {
public void onGroupExpand(int groupPosition_e) {
myView.getChildAt(groupPosition_e).setBackgroundDrawable(expandBG);
}
});

有人知道怎么做吗?

最佳答案

我认为您应该制作自己的适配器,它扩展了 SimpleExpandableListAdapter。在那个类中你应该覆盖

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View result = super.getGroupView(groupPosition, isExpanded, convertView, parent);

if (isExpanded) {
result.setBackgroundDrawable(expandBG);
} else {
result.setBackgroundDrawable(collapseBG);
}

return result;
}

在 onCreate 函数中你应该像下面这样写:

myView.setlistAdapter (new YourExtendedSimpleExpandableListAdapter( ...... ));

希望对你有帮助

关于Android expandablelistview组/父背景图片在折叠或展开时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3081881/

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