[英]Android, Expandable List View, Remove child from single entry
目前,我在從可擴展列表視圖中刪除UI元素時遇到問題。 我在我的getChildView()中使用它
View button = v.findViewById(R.id.moreInfoButton1);
if(button != null)
{
((ViewManager)button.getParent()).removeView(button);
}
現在,我意識到我實際上是從布局模板中刪除了元素,這意味着在擴展另一個元素時,應用程序崩潰,因為找不到按鈕。 因此,有一種方法可以僅從單個條目中刪除元素。
您可以在數據集中設置一個標志,並且當您想要刪除(隱藏)該元素時,將該元素的標志設置為true
,然后調用notifyDataSetChanged
並在您的child的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
}
以及當您想要切換特定項目的可見性時:
yourExpandableListData.get(parentPosition).get(childPosition).setFlag(true);
yourExpandableListAdapter.notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.