Android,可扩展列表视图,从单个条目中删除子项

时间:2014-11-24 22:18:05

标签: android expandablelistview

目前我遇到了从可扩展列表视图中删除UI元素的问题。我在我的getChildView()

中使用它
View button = v.findViewById(R.id.moreInfoButton1);

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

现在我意识到我实际上是从布局模板中删除元素,这意味着当展开另一个元素时,应用程序崩溃,因为它无法找到按钮。那么有没有一种方法可以从单个条目中删除元素。

1 个答案:

答案 0 :(得分:0)

您可以在数据集中设置一个标志,当您想删除(隐藏)该元素时,将该元素的标记设置为true,然后调用notifyDataSetChanged并在{{1儿童的方法,在给视图充气时,检查一下该标志是否getView显示该元素!

示例:

适配器中的

true

并将此字段添加到您的数据中:

...
@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);
   }
   ...
}  

当您想要切换特定项目的可见性时:

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