How to implement select all button to select all items in recyclerview using actionmode and selectiontracker?

487 Views Asked by At

I have this code in my MainActivity

    recyclerView.setAdapter(customAdapter);
    customAdapter.submitList(path_list);

    selectionTracker = new SelectionTracker.Builder<>(
            "my-selection-id",
            recyclerView,
            new ScrollKeyProvider(1, path_list),
            new ScrollItemDetailsLookup(recyclerView),
            StorageStrategy.createLongStorage()
    )
            .withOnItemActivatedListener(new OnItemActivatedListener<Long>() {
                @Override
                public boolean onItemActivated(@NonNull ItemDetailsLookup.ItemDetails<Long> item, @NonNull MotionEvent e) {
                    Log.d("TAG", "Selected ItemId: " + item.toString());
                    return true;
                }
            })
            .withOnDragInitiatedListener(new OnDragInitiatedListener() {
                @Override
                public boolean onDragInitiated(@NonNull MotionEvent e) {
                    Log.d("TAG", "onDragInitiated");
                    return true;
                }
            }).build();

    customAdapter.setSelectionTracker(selectionTracker);

    selectionTracker.addObserver(new SelectionTracker.SelectionObserver() {
        @Override
        public void onItemStateChanged(@NonNull Object key, boolean selected) {
            super.onItemStateChanged(key, selected);
        }

        @Override
        public void onSelectionRefresh() {
            super.onSelectionRefresh();
        }

        @Override
        public void onSelectionChanged() {
            super.onSelectionChanged();
            if (selectionTracker.hasSelection() && actionMode == null) {
                actionMode = startSupportActionMode(new ActionModeController(ScrollActivity.this, selectionTracker));
                actionMode.getMenu().findItem(R.id.action_item_count).setTitle("" + selectionTracker.getSelection().size());
            } else if (!selectionTracker.hasSelection() && actionMode != null) {
                actionMode.finish();
                actionMode = null;
            } else {
                actionMode.getMenu().findItem(R.id.action_item_count).setTitle("" + selectionTracker.getSelection().size());
            }
            Iterator<String> itemIterable = selectionTracker.getSelection().iterator();
            while (itemIterable.hasNext()) {
                Log.i("TAG", itemIterable.next());
            }
        }

        @Override
        public void onSelectionRestored() {
            super.onSelectionRestored();
        }
    });

This is my ActionMode callback code

public class ActionModeController implements ActionMode.Callback {

private final Context context;
private final SelectionTracker selectionTracker;

public ActionModeController(Context context, SelectionTracker selectionTracker) {
    this.context = context;
    this.selectionTracker = selectionTracker;
}

@Override
public boolean onCreateActionMode(androidx.appcompat.view.ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.action_menu, menu);
    return true;
}

@Override
public boolean onPrepareActionMode(androidx.appcompat.view.ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(androidx.appcompat.view.ActionMode mode, MenuItem item) {
    if(item.getItemId()==R.id.action_clear){
        if (selectionTracker.hasSelection()){
            selectionTracker.clearSelection();
        }
    }
    else if(item.getItemId()==R.id.action_select_all){
       // **THIS IS PLACE WHERE I NEED HELP TO ENTER CODE FOR SELECT ALL FUNCTIONALITY**
    }
    return false;
}

@Override
public void onDestroyActionMode(androidx.appcompat.view.ActionMode mode) {
    selectionTracker.clearSelection();
}

}

What should I do in select all section in onActionItemClicked for selecting all items in Recyclerview using SelectionTracker?

You can see for the clear option in onActionItemClicked, I have used functions of selectionTracker to clear all the selected items. I am expecting similar solution for select all option too.

1

There are 1 best solutions below

0
Michael Carroll On

Kotlin version using PagingDataAdapter - can be reworked replacing the snapshot to items in your implementation.

var itemsArray = arrayListOf<Long>()
adapter?.snapshot()?.items?.forEach {
    if (!selectionTracker.isSelected(it.id.toLong()))
        itemsArray.add(it.id.toLong())
}
selectionTracker?.setItemsSelected(itemsArray.asIterable(), true)