I Am using groupie expandable recyclerview, I only have to expand current group and also have to collapse previous expanded group, to be precise at a time there should be only one group expanded.. currently I'm using ontoggleExpanded() method how can I achieve above please post an answer
我使用的是追星族可扩展回收视图,我只需要扩展当前组,也必须折叠以前扩展的组,准确地说,一次应该只有一个组扩展。目前我正在使用ontoggleExpanded()方法,我如何实现上述功能,请张贴答案
I'm using ontoggleExpanded() method of "groupie" library but I only have to expand one Group at a time
currently it expands all groups what code should I change
我使用的是“GROUPIE”库的ontoggleExpanded()方法,但我一次只需要展开一个组,目前它会展开所有组,我应该更改哪些代码
更多回答
优秀答案推荐
A kotlin solution was provided by ValCanBuild in the library's github issues:
https://github.com/lisawray/groupie/issues/301 - The following code is copy-pasted from the issue:
ValCanBuild在该库的giHub问题中提供了一个kotlin解决方案:https://github.com/lisawray/groupie/issues/301-以下代码是从该问题复制粘贴的:
class ExpandableHeaderItem : ExpandableItem {
var onExpandListener: ((ExpandableGroup) -> Unit)? = null
private lateinit var expandableGroup: ExpandableGroup
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
super.bind(viewHolder, position)
// Initial icon state -- not animated.
viewHolder.icon.apply {
...
setOnClickListener {
expandableGroup.onToggleExpanded()
onExpandListener?.invoke(expandableGroup)
....
}
}
....
}
....
}
val testExpGrpSection = Section().apply {
val expandableGroups = mutableListOf<ExpandableGroup>()
for (i in 0..3) {
val header = ExpandableHeaderItem()
val group = ExpandableGroup(header)
// here you would add children to the group
header.onExpandListener = { toggledGroup ->
if (toggledGroup.isExpanded) {
// When a group is expanded, collapse all other groups
expandableGroups.forEach {
if (it != toggledGroup && it.isExpanded) {
it.onToggleExpanded()
}
}
}
}
expandableGroups.add(group)
add(group)
}
}
A java equivalent would be an interface
listener inside your expandable item header:
Java的等价物是可展开项标头中的接口侦听器:
public interface onExpandedListener {
void onStateChanged(ExpandableGroup exGroup, int position);
}
private onExpandedListener listener;
@Override
public void bind(final ViewItemHeaderBinding viewBinding, int position) {
viewBinding
.getRoot()
.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
// must be called in order
// onToggleExpanded then onStateChanged
expandableGroup.onToggleExpanded();
if (listener != null) {
listener.onStateChanged(expandableGroup, position);
}
bindIcon(viewBinding);
}
});
}
Now you need to pass this listener either through the constructor
现在,您需要通过构造函数传递此侦听器
public ExpandableHeaderItem(Model m, onExpandedListener listener) {
this.listener = listener;
}
or add a public setListener method.
或者添加一个公共的setListener方法。
public void setListener(onExpandedListener listener) {
this.listener = listener;
}
Now when you populate the adapter:
现在,当您填充适配器时:
groupAdapter = new GroupAdapter(); expandedGroups = new ArrayList<ExpandableGroup>();
for (Model m : models) {
// add header models to groups
expandableHeaderItem = new ExpandableHeaderItem(model);
ExpandableGroup expandableGroup = new ExpandableGroup(expandableHeaderItem);
// listener when a group is expanded
expandableHeaderItem.setListener(
new ExpandableHeaderItem.onExpandedListener() {
@Override
public void onStateChanged(ExpandableGroup exGroup, int position) {
if (exGroup.isExpanded()) {
for (int i = 0; i < expandedGroups.size(); i++) {
if(expandedGroups.get(i) != exGroup && expandedGroups.get(i).isExpanded() ) {
expandedGroups.get(i).onToggleExpanded();
}
}
}
}
});
//add expandable group to the already expanded groups array
expandedGroups.add(expandableGroup);
// add model groups to adapter
groupAdapter.add(expandableGroup);
}// end of for loop
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
binding.recyclerView.setAdapter(groupAdapter);
更多回答
我是一名优秀的程序员,十分优秀!