简体   繁体   English

如何在ExpandableListView中隐藏特定组的子项?

[英]How to hide child items for a particular group in ExpandableListView?

My ExpandableListView has always single children for a particular group. 我的ExpandableListView始终具有特定组的单个子级。 For some groups I'd like to hide child items however I'd like the group to be still clickable (every group item after click changes it's layout). 对于某些组,我想隐藏子项,但是我希望该组仍然可单击(单击后的每个组项都会更改其布局)。 So what should be returned for such groups from getChildView ? 那么,应该从getChildView为这些组返回什么?

Override getChildrenCount and for your wanted groups return 0, as if they don't have children. 覆盖getChildrenCount ,对于想要的组,返回0,就好像它们没有孩子一样。 They will still be clickable though. 他们仍然将是可点击的。

@Override
public int getChildrenCount(int groupPosition) {
    if (groupPosition == 1) {
        return 0;
    } else {
        return super.getChildrenCount(groupPosition);
    }
}

Here is what i think, the code is not tested 这是我的想法,代码未经过测试

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;
}

You need to call collapseGroup() . 您需要调用crashGroup() Also in your constructor you need your ExpandableListView 同样在您的构造函数中,您需要您的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;
    }

In this case I hide last group, but you can the current group or whatever you want. 在这种情况下,我隐藏了最后一个组,但是您可以当前组或任何您想要的组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM