Pardeep Sharma
Pardeep Sharma

Reputation: 427

How to populate different child under Expandable listview?

I am facing a problem while inflating expandable listview. I want to perform like this :

enter image description here

for this purpose I am taking a listview item like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#112234"
android:orientation="horizontal">

<CheckedTextView
    android:id="@+id/checkedtextViewZoneItem"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:paddingLeft="30dp"
    android:paddingTop="10dp"
    android:gravity="center_vertical"
    android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"/>

<TextView
    android:id="@+id/txtViewZoneItem"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:background="#112234"
    android:textColor="#C3C7D2"
    android:textSize="18dip"
    android:paddingTop="10dp"/>    

and on my UI counterpart I'd put an GroupExpandListener. I am able to get that delete button click. But after clicking on that delete button I am not finding any way to hide/visible the particular group items checkboxes. For group header click I am performing like this :

 // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(final int groupPosition) {

            View parentView =  expListView.findViewWithTag(groupPosition);
            ImageView deleteZoneItem = (ImageView) parentView.findViewById(R.id.imageViewDeleteZoneItems);
            deleteZoneItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    if(!expListView.isGroupExpanded(groupPosition)) {
                        Toast.makeText(ZoneListActivity.this, "expand", Toast.LENGTH_SHORT).show();
                        expListView.expandGroup(groupPosition);
                    }
                }
            });


        }
    });

Please suggest me a workaround either I need to make changes on my customAdapter class or I can perform through my activity class only.

Regards,

Android Geek

Upvotes: 0

Views: 599

Answers (1)

Beloo
Beloo

Reputation: 9925

It can be done in such way: You should have a custom adapter which extends BaseExpandableAdapter; a group object, which contains isDeletable boolean flag, a group name and a list of child objects ( in your case - strings).

When user expands group you can retrive that group from adapter by position and set isDeletable flag if delete button was pressed or not.

In getChildView method of adapter you could also retrive group, check a flag and set visibility of checkbox or sth what you need.

Update, a sample. Look at my custom adapter implementation:

public class CustomAdapter extends BaseExpandableListAdapter {

public static class GroupItem {
    private String groupName;
    private List<String> items;
    private boolean isDeletable;

    public GroupItem(String groupName, List<String> items) {
        this.groupName = groupName;
        this.items = items;
    }

    public String getGroupName() {
        return groupName;
    }
    public List<String> getItems() {
        return items;
    }

    public void setDeletable(boolean isDeletable) {
        this.isDeletable = isDeletable;
    }
}

private List<GroupItem> groups;
private Context context;

public CustomAdapter(Context context, List<GroupItem> groups) {
    this.groups = groups;
    this.context = context;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return getGroup(groupPosition).getItems().size();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup container) {
    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_group, container, false);
    }

    ((TextView) convertView).setText(getGroup(groupPosition).getGroupName());

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup container) {
    ChildViewHolder viewHolder;
    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_child, container, false);
        viewHolder = new ChildViewHolder();
        viewHolder.text = (TextView)convertView.findViewById(R.id.textView);
        viewHolder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ChildViewHolder)convertView.getTag();
    }

    GroupItem group = getGroup(groupPosition);
    String item = getChild(groupPosition, childPosition);
    viewHolder.text.setVisibility(group.isDeletable ? View.GONE : View.VISIBLE);
    viewHolder.checkbox.setVisibility(group.isDeletable ? View.VISIBLE : View.GONE);

    return convertView;
}

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

@Override
public String getChild(int groupPosition, int childPosition) {
    return getGroup(groupPosition).getItems().get(childPosition); }

}

viewHolder.text.setVisibility(group.isDeletable ? View.GONE : View.VISIBLE);
viewHolder.checkbox.setVisibility(group.isDeletable ? View.VISIBLE : View.GONE);

did what you want

You could set isDeletable flag in appropriate group with retriving it with groupPosition ( in your example) and call expandGroup after that

Upvotes: 0

Related Questions