Skip to content
This repository was archived by the owner on Oct 23, 2021. It is now read-only.

Commit 143d3e4

Browse files
committed
fixed the selection rotation state management issues
1 parent 21a1cf5 commit 143d3e4

File tree

7 files changed

+95
-32
lines changed

7 files changed

+95
-32
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
bin/
1313
gen/
1414

15-
# Local configuration file (sdk path, etc)
16-
local.properties
17-
gradle.properties
15+
# Local configuration file (sdk path, etc) just .properties will do
16+
*.properties
17+
1818

1919
# Windows thumbnail db
2020
Thumbs.db
@@ -28,7 +28,7 @@ Thumbs.db
2828

2929
# Android Studio
3030
.idea
31-
.iml
31+
*.iml
3232
#.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
3333
.gradle
3434
build/

criminalintent-sample/src/main/java/com/bignerdranch/android/criminalintent/BaseFragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ protected ActionBar getActionBar() {
1010
return ((ActionBarActivity) getActivity()).getSupportActionBar();
1111
}
1212

13+
14+
1315
}

criminalintent-sample/src/main/java/com/bignerdranch/android/criminalintent/CrimeListFragment.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828

2929
import java.util.ArrayList;
3030

31-
public class CrimeListFragment extends BaseFragment {
32-
31+
public class CrimeListFragment extends BaseFragment {
3332
private RecyclerView mRecyclerView;
3433
private static final String TAG="crimeListFragment";
3534
private MultiSelector mMultiSelector = new MultiSelector();
36-
37-
private ArrayList<Crime> mCrimes;
35+
private ArrayList<Crime> mCrimes;
3836
private boolean mSubtitleVisible;
39-
37+
38+
39+
4040
@Override
4141
public void onCreate(Bundle savedInstanceState) {
4242
super.onCreate(savedInstanceState);
@@ -46,12 +46,33 @@ public void onCreate(Bundle savedInstanceState) {
4646
mSubtitleVisible = false;
4747
}
4848

49+
/**
50+
* Note: since the fragment is retained. the bundle passed in after state is restored is null.
51+
* THe only way to pass parcelable objects is through the activities onsavedInstanceState and appropiate startup lifecycle
52+
* However after having second thoughts, since the fragment is retained then all the states and instance variables are
53+
* retained as well. no need to make the selection states percelable therefore just check for the selectionstate
54+
* from the multiselector
55+
*/
56+
@Override
57+
public void onActivityCreated(Bundle savedInstanceState) {
58+
super.onActivityCreated(savedInstanceState);
59+
if(mMultiSelector.isSelectable()){
60+
if(mDeleteMode !=null){
61+
mDeleteMode.setClearOnPrepare(!mDeleteMode.shouldClearOnPrepare());
62+
beginActionMode();
63+
64+
65+
}
66+
67+
}
68+
}
69+
4970
@TargetApi(11)
5071
@Override
5172
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
5273
View v = inflater.inflate(R.layout.fragment_recyclerview, parent, false);
5374

54-
if (mSubtitleVisible) {
75+
if (mSubtitleVisible) {
5576
getActionBar().setSubtitle(R.string.subtitle);
5677
}
5778

@@ -60,7 +81,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle saved
6081
mCrimes = CrimeLab.get(getActivity()).getCrimes();
6182
mRecyclerView.setAdapter(new CrimeAdapter());
6283

63-
6484
return v;
6585
}
6686

@@ -103,7 +123,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
103123
}
104124
}
105125

106-
private ActionMode.Callback mDeleteMode = new ModalMultiSelectorCallback(mMultiSelector) {
126+
private ModalMultiSelectorCallback mDeleteMode = new ModalMultiSelectorCallback(mMultiSelector) {
107127

108128
@Override
109129
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
@@ -179,8 +199,7 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn
179199
}
180200

181201

182-
private class CrimeHolder extends SwappingHolder
183-
implements View.OnClickListener, View.OnLongClickListener {
202+
private class CrimeHolder extends SwappingHolder {
184203
private final TextView mTitleTextView;
185204
private final TextView mDateTextView;
186205
private final CheckBox mSolvedCheckBox;
@@ -193,8 +212,7 @@ public CrimeHolder(View itemView) {
193212
mDateTextView = (TextView) itemView.findViewById(R.id.crime_list_item_dateTextView);
194213
mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.crime_list_item_solvedCheckBox);
195214
itemView.setOnClickListener(this);
196-
itemView.setOnLongClickListener(this);
197-
itemView.setLongClickable(true);
215+
198216
}
199217

200218
public void bindCrime(Crime crime) {
@@ -206,21 +224,28 @@ public void bindCrime(Crime crime) {
206224

207225
@Override
208226
public void onClick(View v) {
209-
if (mCrime == null) {
227+
super.onClick(v);
228+
if (mCrime == null || mMultiSelector.isSelectable()) {
210229
return;
211230
}
212-
if (!mMultiSelector.tapSelection(this)) {
213-
selectCrime(mCrime);
214-
}
231+
232+
selectCrime(mCrime);
233+
215234
}
216235

217236
@Override
218-
public boolean onLongClick(View v) {
219-
ActionBarActivity activity = (ActionBarActivity)getActivity();
220-
activity.startSupportActionMode(mDeleteMode);
221-
mMultiSelector.setSelected(this, true);
222-
return true;
237+
public void beginSelectionActionMode() {
238+
beginActionMode();
239+
223240
}
241+
242+
243+
}
244+
245+
private void beginActionMode() {
246+
ActionBarActivity activity = (ActionBarActivity)getActivity();
247+
activity.startSupportActionMode(mDeleteMode);
248+
224249
}
225250

226251
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {

recyclerview-multiselect/src/main/java/android/support/v7/widget/RebindReportingHolder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ private void checkFlags(int setFlags) {
4646
}
4747
}
4848

49+
/**
50+
* check if the view is due for rebiding
51+
* @param flag
52+
* @return
53+
*/
4954
private static boolean isRelevantFlagSet(int flag) {
50-
for (Integer value : new int[] { FLAG_BOUND, FLAG_CHANGED, FLAG_UPDATE, FLAG_RETURNED_FROM_SCRAP }) {
55+
for (Integer value : new int[] { FLAG_BOUND, FLAG_CHANGED, FLAG_UPDATE, FLAG_RETURNED_FROM_SCRAP ,FLAG_TMP_DETACHED}) {
5156
if ((flag & value) == value) {
5257
return true;
5358
}

recyclerview-multiselect/src/main/java/com/bignerdranch/android/multiselector/ModalMultiSelectorCallback.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* and {@link android.support.v7.view.ActionMode.Callback#onDestroyActionMode(android.support.v7.view.ActionMode)}.</p>
1212
*/
1313
public abstract class ModalMultiSelectorCallback implements ActionMode.Callback {
14-
private MultiSelector mMultiSelector;
1514

15+
private MultiSelector mMultiSelector;
16+
private static final String TAG="modalMultiSelectorCallback";
1617
private boolean mClearOnPrepare = true;
1718

1819
public ModalMultiSelectorCallback(MultiSelector multiSelector) {
@@ -66,4 +67,7 @@ public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
6667
public void onDestroyActionMode(ActionMode actionMode) {
6768
mMultiSelector.setSelectable(false);
6869
}
70+
71+
72+
6973
}

recyclerview-multiselect/src/main/java/com/bignerdranch/android/multiselector/MultiSelector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
public class MultiSelector {
3636
private SparseBooleanArray mSelections = new SparseBooleanArray();
3737
private WeakHolderTracker mTracker = new WeakHolderTracker();
38-
39-
private boolean mIsSelectable;
38+
private boolean mIsSelectable;
4039

4140
/**
4241
* <p>Toggle whether this MultiSelector is in selection mode or not.
@@ -196,4 +195,6 @@ private void refreshHolder(SelectableHolder holder) {
196195
boolean isActivated = mSelections.get(holder.getPosition());
197196
holder.setActivated(isActivated);
198197
}
198+
199+
199200
}

recyclerview-multiselect/src/main/java/com/bignerdranch/android/multiselector/SwappingHolder.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
*
3434
* <p>(Thanks to <a href="https://github.com/kurtisnelson/">Kurt Nelson</a> for examples and discussion on approaches here.)</p>
3535
*/
36-
public class SwappingHolder extends MultiSelectorBindingHolder implements SelectableHolder {
37-
/**
36+
public abstract class SwappingHolder extends MultiSelectorBindingHolder implements SelectableHolder,View.OnClickListener, View.OnLongClickListener {
37+
/**
3838
* <p>Construct a new SelectableHolder hooked up to be controlled by a MultiSelector.</p>
3939
*
4040
* <p>If the MultiSelector is not null, the SelectableHolder can be selected by
@@ -49,7 +49,8 @@ public class SwappingHolder extends MultiSelectorBindingHolder implements Select
4949
*/
5050
public SwappingHolder(View itemView, MultiSelector multiSelector) {
5151
super(itemView, multiSelector);
52-
52+
this.itemView.setOnLongClickListener(this);
53+
this.itemView.setLongClickable(true);
5354
mMultiSelector = multiSelector;
5455
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
5556
setSelectionModeStateListAnimator(getRaiseStateListAnimator(itemView.getContext()));
@@ -284,4 +285,29 @@ private static StateListAnimator getRaiseStateListAnimator(Context context) {
284285
return null;
285286
}
286287
}
288+
289+
/**
290+
* just few cleanup to handle click event
291+
* Note: subclass should call super.onClick before implementation of their onClick
292+
*/
293+
@Override
294+
public void onClick(View v) {
295+
if (!mMultiSelector.tapSelection(this)) {
296+
return;
297+
}
298+
}
299+
300+
/**
301+
*
302+
*
303+
* just few cleanup to handle starting up the selection mode
304+
* Note: subclass should call super.onLongClick before implementation of their onLongClick
305+
*/
306+
@Override
307+
public boolean onLongClick(View v) {
308+
beginSelectionActionMode();
309+
mMultiSelector.setSelected(this, true);
310+
return true;
311+
}
312+
public abstract void beginSelectionActionMode();
287313
}

0 commit comments

Comments
 (0)