在我的expandableListView中,我创建了一个自定义按钮来扩展/折叠组并扩展它可以工作,但是在折叠时没有。
使用此代码
listView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Log.d("group click");
return true;
}
});
listView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Log.d("group collapse");
}
});
listView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Log.d("group expand");
}
});
使用此代码: 当群组崩溃时:
setOnGroupClickListener
扩展群组时:
setOnGroupClickListener
单击展开的组时,为什么未加载setOnGroupClickListener
?
怎么解决?
答案 0 :(得分:1)
我遇到了一个类似的问题。我需要所有组都要扩展和可点击。为了使它工作,我在我的ExpandableListActivity
:
public void onCreate(Bundle savedInstanceState) {
...
// Expanding all.
for(int i = 0; i < adapter.getGroupCount(); i++)
getExpandableListView().expandGroup(i);
...
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
overrideStupidListBehaviour(groupPosition);
return false;
}
@Override
public void onGroupCollapse(int groupPosition) {
// Forbidding it to collapse.
getExpandableListView().expandGroup(groupPosition);
overrideStupidListBehaviour(groupPosition);
}
private void overrideStupidListBehaviour(int groupPosition) {
// Code to do when onGroupClick is called
}
如果有正常的方法,我真的很感兴趣。
答案 1 :(得分:1)
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}});
答案 2 :(得分:1)
使用这种方式非常简单
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if(isExpanded){
//do something when expanded
}else{
//do something when collapsed or not expanded
}
...........
}
答案 3 :(得分:0)
如果你点击一个组,它应该每次都被调用,但如果你的按钮或其他可点击或可聚焦的东西在组视图中,那么它只会触发对象的onClick方法。如果这不是解决方案,请提供更多代码。
@folone 还有更好的方法:
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
doSomething();
return true;
}
return true 应取消大多数默认内容 - &gt;展开/折叠