简体   繁体   English

Android,展开式列表视图,从单个条目中删除子级

[英]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. 目前,我在从可扩展列表视图中删除UI元素时遇到问题。 I'm using this in my getChildView() 我在我的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! 您可以在数据集中设置一个标志,并且当您想要删除(隐藏)该元素时,将该元素的标志设置为true ,然后调用notifyDataSetChanged并在您的child的getView方法中,在放大视图时检查该标志如果是true请不要显示该元素!

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();

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

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