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

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.



Solution 1:[1]

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)

Solution 2:[2]

As discussed above you can use the following to record any errors which will be returned to you in the terminal :

import subprocess

a = subprocess.run(["python3","hello.py"], stdout=subprocess.PIPE)
print(a.stdout.decode('utf-8'))

Once you have the output, you can search for errors ad log your experiments accordingly .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kozmotronik
Solution 2 Atharva Gundawar