简体   繁体   中英

Android, Expandable List View, Remove child from single entry

At the moment I'm running into issues with removing a UI element from an expandable List view. I'm using this in my getChildView()

View button = v.findViewById(R.id.moreInfoButton1);

if(button != null)
{
    ((ViewManager)button.getParent()).removeView(button);
}

Now I realize that I'm actually removing the element from the layout template, which means when expanding another element, the app crashes as it can't find the button. So is there a way of removing an element just from a single entry.

you can set a flag in your data set and when you want to remove (hide) that element, set the flag of that element to true and then call notifyDataSetChanged and in your getView method for child, when inflating your view, check for that flag and if it was true don't show that element!

example :

in your adapter :

...
@Override
public View getChildView(int groupposition, int childpostion, boolean isLastchild, View convertview,
        ViewGroup parent) {
   ...
   if (data.get(groupposition).get(childposition).getFlag()) {
        yourView.setVisibility(View.GONE);
   } else {
        yourView.setVisibility(View.VISIBLE);
   }
   ...
}  

and add this field to your data :

public class ChildData {
   ...
   boolean flag;
   // getter and setter 
}

and when you want to toggle visibility of particular item :

yourExpandableListData.get(parentPosition).get(childPosition).setFlag(true);
yourExpandableListAdapter.notifyDataSetChanged();

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