简体   繁体   中英

Android ExpandableListView: remove or hide a childview from group after clicking a button of the childview

I have an BaseExpandableListAdapter where its first Group has elements with Buttons . I want to remove or hide the Child view after clicking any of its Button . I have tried these in following code.

public class ProfileExpandableAdapter extends BaseExpandableListAdapter{

private Context context;
private ArrayList<ExpandableGroup> groups;
private ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private String type = "update_user_friend", userName = "";
private JSONParser jsonParser;
private String wpedenUrl = "", TAG_SUCCESS = "success";

public ProfileExpandableAdapter(Context context, ArrayList<ExpandableGroup> groups, String userName) {
    this.context = context;
    this.groups = groups;
    this.userName = userName;
    jsonParser = new JSONParser();
    wpedenUrl = context.getResources().getString(R.string.site);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<ExpandableChild> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final ExpandableChild child = (ExpandableChild) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.wpedenyo_profile_expandable_group_child, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.wpedenyo_profile_group_child);
    tv.setText(child.getName().toString());
    String image = child.getThumbnailUrl();
    if(groupPosition == 0){
        NetworkImageView notifiedUserImage = (NetworkImageView) convertView.findViewById(R.id.wpedenyo_notified_friend_image);
        notifiedUserImage.setVisibility(View.VISIBLE);
        notifiedUserImage.setImageUrl(image, imageLoader);
        Button confirmButton = (Button) convertView.findViewById(R.id.wpedenyo_profile_button_confirm);
        confirmButton.setVisibility(View.VISIBLE);
        Button cancelButton = (Button) convertView.findViewById(R.id.wpedenyo_profile_button_cancel);
        cancelButton.setVisibility(View.VISIBLE);

        confirmButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                groups.remove(child);
                notifyDataSetChanged();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                groups.remove(child);
                notifyDataSetChanged();
            }
        });
    }

    return convertView;
}

public void removeChild(int groupPosition, int childPosition) {

    if (getChildrenCount(groupPosition)>0 && getChildrenCount(groupPosition)-1 >= childPosition )
    {
        Object task = this.getChild(groupPosition, childPosition);
        groups.remove(task);
        this.notifyDataSetChanged();
    }
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<ExpandableChild> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    ExpandableGroup group = (ExpandableGroup) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.wpedenyo_profile_expandable_group, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.wpedenyo_profile_list_header);
    tv.setText(group.getName());
    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

But after clicking Button it cann't remove Child from Group . How can i do that? Thanks in advance.

Instead of groups.remove(child) (I think you're trying to remove group here) try something like

  1. get current group

    ExpandableGroup currentGroup=groups.get(groupPosition);

  2. get the children (you have to implement getChildren method): currentGroup.getChildren().remove(child);

Hope it helps.

You need to call collapseGroup() . Also in your constructor you need your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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