Some questions re sample project ActionModeList2
from the CommonsWare Community archivesAt July 12, 2018, 5:23am, Shahood asked:
Hello,
I’m trying to understand the implementation of ActionMode using RecyclerView and am kind of stuck at the sample project ActionModeList2 in chapter Advanced RecyclerView under the head Changing the Contents (Pg 1537).
Here are some questions in this regard. I found it better to keep all questions under the same thread.
-
In MainActivity, is it necessary to reassign
activeMode=mode;
inonCreateActionMode()
whenactiveMode
has already been assigned a value in overridden methodonChecked()
viaactiveMode=startActionMode(this)
; which actually triggered the methodonCreateActionMode()
? -
In MainActivity(), was it equally good to simply use
clearChecks();
instead of the following inonDestroyActionMode()
:visitChecks(new ChoiceMode.Visitor() { @Override public void onCheckedPosition(int position) { onChecked(position, false); notifyItemChanged(position); } });
-
I was trying to find out how
actionMode
is maintained across orientation changes in this sample app, because I was unable to find a call tostartActionMode()
inonRestoreInstanceState()
. Eventually I found out that, in MainActivity, when we calladapter.onRestoreInstanceState(state);
inonRestoreInstanceState()
, we are supplying the savedParcelableSparseBoolenArray checkStates
(defined in class MultiChoiceMode) to the adapter which will later be used by the RowController in itsbindModel()
viacb.setChecked(adapter.isChecked(getAdapterPosition()));
. Thiscb.setChecked()
triggersonCheckedChanged()
which callsadapter.onChecked(getAdapterPosition(), isChecked);
wherestartActionMode()
is called when first checked item is loaded on screen. Phew!
But here a question comes to mind: wheneveradapter.onChecked(getAdapterPosition(), isChecked);
(in this case, viaonCheckedChanged()
), we are eventually calling overridden methodsetChecked()
of MultiChoiceMode which is either saving the checked position in or deleting the unchecked position from the sameParcelableSparseBoolenArray checkStates
. While it is required in case the user is actually checking or unchecking the list items but becomes a mere duplication (putting the values incheckStates
that it already has) in case checked items are loaded to restore ActionMode in case of orientation change OR when user simply scrolls the list up and down andbindModel()
method is recalled.
Is above mentioned understanding regarding duplication in putting/deleting checked and unchecked positions from checkedStates, correct and is there a better approach for achieving the same objective?
At July 12, 2018, 10:59am, mmurphy replied:
First, please understand that I wrote this code ~3 years ago, with minor bug fixes since then. Also, please note that Google is finally offering their own RecyclerView
selection library, which you may wish to examine.
-
You are correct that this is probably a duplicate assignment.
-
I seem to recall that I used
clearChecks()
originally and changed it to this in response to a bug report. -
This did not seem to contain a question, and so I do not know what to answer. My apologies if I misunderstood what you asked. Again, now that Google has a selection library, please consider it.
At July 12, 2018, 11:38am, Shahood replied:
While I’ll look into android’s library, can u pl look at the third question once again. I’ve added a question in the end this time. Just need a confirmation from your side of my understanding and also a suggestion for a better approach.
Thanks for your time.
At July 12, 2018, 11:40am, Shahood replied:
Also, can u pl post a link to android’s selection library.
At July 12, 2018, 11:44am, Shahood replied:
Found it…that’s part of AndroidX. Will wait till it’s incorporated in your book.
At July 12, 2018, 11:52am, mmurphy replied:
Is above mentioned understanding regarding duplication in putting/deleting checked and unchecked positions from checkedStates
Ideally, there should be no changes to the states on mere scrolling, and configuration changes should replace the entire set of checked states.
is there a better approach for achieving the same objective?
I have no idea, sorry.