mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.85k stars 494 forks source link

Multiselect + Subitems + Deletion problem #193

Closed MFlisar closed 8 years ago

MFlisar commented 8 years ago

Based on your multi select and expandable list example, I'm trying to combine those two - I think I did not change anything relevant.

Showing the data with headers works fine, expanding/collapsing works fine. BUT as soon as I start deleting something, it get's messed up.

Observations

Problem

Afterwards, more problems occur (expanding a header that still exists will show selected items for example, ...)

How I set up my adapter

    mFastAdapter = new FastItemAdapter<>();
    mActionModeHelper = new ActionModeHelper(mFastAdapter, R.menu.menu_folders, new ActionBarCallBack());

    // 2) FastAdapter Setup
    mFastAdapter.withSelectable(true);
    mFastAdapter.withMultiSelect(true);
    mFastAdapter.withSelectOnLongClick(true);
    mFastAdapter.withOnPreClickListener(new FastAdapter.OnClickListener<IItem>() {
        @Override
        public boolean onClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
            //we handle the default onClick behavior for the actionMode. This will return null if it didn't do anything and you can handle a normal onClick
            Boolean res = mActionModeHelper.onClick(item);
            return res != null ? res : false;
        }
    });
    mFastAdapter.withOnPreLongClickListener(new FastAdapter.OnLongClickListener<IItem>() {
        @Override
        public boolean onLongClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
            ActionMode actionMode = mActionModeHelper.onLongClick(mActivity, position);

            if (actionMode != null) {
                //we want color our CAB
                mActivity.findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.colorPrimary, R.color.material_drawer_primary));
            }

            //if we have no actionMode we do not consume the event
            return actionMode != null;
        }
    });

How I fill my data

    // List of sub items without headers
    List<FolderItem> folders = ...;

    // Test - group items in groups of 10
    List<IItem> items = new ArrayList<>();
    int count = folders.size();
    int headers = (int)Math.ceil((float)folders.size() / 10f);
    for (int i = 0; i < headers; i++)
    {
        FolderItemHeader header = new FolderItemHeader()
                .withHeader("Header " + (i + 1))
                .withName("Header " + (i + 1))
                .withDescription("Description...")
                .withSubItems(folders.subList(i * 10, Math.min(count, i * 10 + 10)))
                // ALL folder items have an id that represents the rowid in the database, so
                // the ids are > 0 AND unique for sure!
                // using ids < 0 will result in no confilcting ids for sure
                .withIdentifier(-1 - i)
                .withIsExpanded(true);
        items.add(header);
    }

    // set data
    FastItemAdapter<IItem> fastAdapter = (FastItemAdapter<IItem>)mBinding.rvData.getAdapter();
    fastAdapter.setNewList(items);