Android,可扩展列表视图按钮上的删除子项单击

时间:2016-02-20 05:53:20

标签: java android listview expandablelistview

我已经在Android中实现了可扩展列表视图,你可以在屏幕截图中看到,它工作正常,并且在可扩展列表中我有多个具有删除选项的子组,你可以在屏幕截图中看到,我想要要删除用户选择删除的特定子项,我无法删除子表单可扩展列表,我将发布我的代码,请任何人指导我 enter image description here

    public class PendingFragment extends Fragment {

    private AnimatedExpandableListView listView;
    private PendingAdapter adapter;
    CoordinatorLayout coordinatorLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pending, container, false);

        coordinatorLayout = (CoordinatorLayout)view.findViewById(R.id.pending_frag_coordinatorLayout);

        List<GroupItem> items = new ArrayList<GroupItem>();

        // Populate our list with groups and it's children
        for(int i = 1; i < 10; i++) {
            GroupItem item = new GroupItem();

            item.title = "GroupItem " + i;

            for(int j = 0; j < i; j++) {
                ChildItem child = new ChildItem();
                child.title = "ChildItem " + j;

                item.items.add(child);
            }

            items.add(item);
        }

        adapter = new PendingAdapter(getActivity());
        adapter.setData(items);

        listView = (AnimatedExpandableListView)view.findViewById(R.id.pending_explistView);
        listView.setAdapter(adapter);


        return view;
    }

}

我的Listview适配器

    public class PendingAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    private LayoutInflater inflater;

    private List<GroupItem> items;

    ListPopupWindow listPopupWindow;
    String[] products = {"Delete"};
    Context contexts;

    public PendingAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.contexts = context;
    }

    public void setData(List<GroupItem> items) {
        this.items = items;
    }

    private class ChildHolder {
        TextView title;
        ImageView option_menu;
    }

    private class GroupHolder {
        TextView title;
    }
    @Override
    public ChildItem getChild(int groupPosition, int childPosition) {
        return items.get(groupPosition).items.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getRealChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        ChildItem item = getChild(groupPosition, childPosition);
        Log.e("groupPosition=" + groupPosition, "childPosition=" + childPosition);
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = inflater.inflate(R.layout.expchildlistview, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.txtListChild);
            holder.option_menu = (ImageView) convertView.findViewById(R.id.checkin_option_menu);
            convertView.setTag(holder);

        } else {
            holder = (ChildHolder) convertView.getTag();
        }

        holder.title.setText(item.title);
        holder.option_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShowListMenu(view,groupPosition, childPosition);
            }
        });

        return convertView;
    }

    public void ShowListMenu(View v, final int childPosition, final int groupPosition){
        listPopupWindow = new ListPopupWindow(contexts);
        listPopupWindow.setAdapter(new ArrayAdapter(contexts, android.R.layout.simple_list_item_1, products));
        listPopupWindow.setAnchorView(v);
        if (Utils.device_width(contexts) >= 320 && Utils.device_width(contexts) < 480) {
            //Log.v(">= 320 < 480=", ">= 320 < 480");
            listPopupWindow.setWidth(150);
        }else if (Utils.device_width(contexts) >= 480 && Utils.device_width(contexts) < 500) {
            //Log.v(">= 480=", "< 500");
            listPopupWindow.setWidth(150);
        }else{
            listPopupWindow.setWidth(200);
        }
        listPopupWindow.setModal(true);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.v("item.remove=", "" + items.get(groupPosition).items.get(childPosition));
items.get(groupPosition).items.remove(childPosition);
notifyDataSetChanged();
                listPopupWindow.dismiss();

            }
        });
        listPopupWindow.show();
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return items.get(groupPosition).items.size();
    }

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        GroupItem item = getGroup(groupPosition);
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = inflater.inflate(R.layout.explist_group, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }

        holder.title.setText(item.title);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
public class GroupItem {

    public String title;
    public List<ChildItem> items = new ArrayList<ChildItem>();

}
public class ChildItem {
    public String title;
}

1 个答案:

答案 0 :(得分:0)

单击删除时无法看到items.remove(child)。我认为您应该尝试从列表中删除子项,而不是使用更新的值设置适配器。 @Achin