如何在 ExpandableListView 中隐藏特定组的子项?
How to hide child items for a particular group in ExpandableListView?
我的 ExpandableListView
总是有一个特定群体的独生子女。对于某些组,我想隐藏子项,但我希望该组仍然可以单击(单击后的每个组项都会更改其布局)。那么对于来自 getChildView
的此类组应该返回什么?
覆盖 getChildrenCount
并为您想要的组 return 0,就好像他们没有 children。不过它们仍然可以点击。
@Override
public int getChildrenCount(int groupPosition) {
if (groupPosition == 1) {
return 0;
} else {
return super.getChildrenCount(groupPosition);
}
}
这是我的想法,代码没有测试
public view getChildView(int groupPosition, int ChildPosition, boolean b, View view, ViewGroup viewGroup){
if(!groupPostion)//for the group you dont want the layout to be displayed
v=your custom view for the child items.//or if you know the exact position define it like
//if or use switch (groupPosition==something){v.setVisibility(View.GONE)}
return v;
}
您需要致电collapseGroup()。同样在您的构造函数中,您需要您的 ExpandableListView
public class YourExapandableAdapter extends BaseExpandableListAdapter {
private Context mContext;
ExpandableListView mExpandableListView;
int mLastExpandedGroupPosition = -1;
public YourExapandableAdapter(Context context, ExpandableListView expandableListView) {
mContext = context;
mExpandableListView = expandableListView;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
if(groupPosition != mLastExpandedGroupPosition){
mExpandableListView.collapseGroup(mLastExpandedGroupPosition);
}
mLastExpandedGroupPosition = groupPosition;
}
在这种情况下,我隐藏了最后一个组,但您可以隐藏当前组或任何您想要的组。
我的 ExpandableListView
总是有一个特定群体的独生子女。对于某些组,我想隐藏子项,但我希望该组仍然可以单击(单击后的每个组项都会更改其布局)。那么对于来自 getChildView
的此类组应该返回什么?
覆盖 getChildrenCount
并为您想要的组 return 0,就好像他们没有 children。不过它们仍然可以点击。
@Override
public int getChildrenCount(int groupPosition) {
if (groupPosition == 1) {
return 0;
} else {
return super.getChildrenCount(groupPosition);
}
}
这是我的想法,代码没有测试
public view getChildView(int groupPosition, int ChildPosition, boolean b, View view, ViewGroup viewGroup){
if(!groupPostion)//for the group you dont want the layout to be displayed
v=your custom view for the child items.//or if you know the exact position define it like
//if or use switch (groupPosition==something){v.setVisibility(View.GONE)}
return v;
}
您需要致电collapseGroup()。同样在您的构造函数中,您需要您的 ExpandableListView
public class YourExapandableAdapter extends BaseExpandableListAdapter {
private Context mContext;
ExpandableListView mExpandableListView;
int mLastExpandedGroupPosition = -1;
public YourExapandableAdapter(Context context, ExpandableListView expandableListView) {
mContext = context;
mExpandableListView = expandableListView;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
if(groupPosition != mLastExpandedGroupPosition){
mExpandableListView.collapseGroup(mLastExpandedGroupPosition);
}
mLastExpandedGroupPosition = groupPosition;
}
在这种情况下,我隐藏了最后一个组,但您可以隐藏当前组或任何您想要的组。