Skip to content

Commit 21301b9

Browse files
fix deselect after select bug, added save rotations, restore fragment in sample before backPressed
1 parent 5f1ffcc commit 21301b9

4 files changed

Lines changed: 41 additions & 188 deletions

File tree

.gitignore

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,7 @@
33
/local.properties
44
/.idea/workspace.xml
55
/.idea/libraries
6-
.DS_Store
76
/build
87
/captures
9-
.externalNativeBuild
10-
### JetBrains template
11-
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
12-
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
13-
14-
# User-specific stuff:
15-
.idea/workspace.xml
16-
.idea/tasks.xml
17-
.idea/dictionaries
18-
.idea/vcs.xml
19-
.idea/jsLibraryMappings.xml
20-
21-
# Sensitive or high-churn files:
22-
.idea/dataSources.ids
23-
.idea/dataSources.xml
24-
.idea/dataSources.local.xml
25-
.idea/sqlDataSources.xml
26-
.idea/dynamic.xml
27-
.idea/uiDesigner.xml
28-
29-
# Gradle:
30-
.idea/gradle.xml
31-
.idea/libraries
32-
33-
# Mongo Explorer plugin:
34-
.idea/mongoSettings.xml
35-
36-
## File-based project format:
37-
*.iws
38-
39-
## Plugin-specific files:
40-
41-
# IntelliJ
42-
/out/
43-
44-
# mpeltonen/sbt-idea plugin
45-
.idea_modules/
46-
47-
# JIRA plugin
48-
atlassian-ide-plugin.xml
49-
50-
# Crashlytics plugin (for Android Studio and IntelliJ)
51-
com_crashlytics_export_strings.xml
52-
crashlytics.properties
53-
crashlytics-build.properties
54-
fabric.properties
55-
### Android template
56-
# Built application files
8+
/.idea
579
*.apk
58-
*.ap_
59-
60-
# Files for the ART/Dalvik VM
61-
*.dex
62-
63-
# Java class files
64-
*.class
65-
66-
# Generated files
67-
bin/
68-
gen/
69-
out/
70-
71-
# Gradle files
72-
.gradle/
73-
build/
74-
75-
# Local configuration file (sdk path, etc)
76-
local.properties
77-
78-
# Proguard folder generated by Eclipse
79-
proguard/
80-
81-
# Log Files
82-
*.log
83-
84-
# Android Studio Navigation editor temp files
85-
.navigation/
86-
87-
# Android Studio captures folder
88-
captures/
89-
90-
# Intellij
91-
92-
# Keystore files
93-
*.jks
94-
.idea/
95-
gradle/
96-
gradlew

app/src/main/java/com/cleveroad/testrecycler/ui/activities/main_activity/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ protected void onCreate(Bundle savedInstanceState) {
3131

3232
@Override
3333
public void onBackPressed() {
34+
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.root);
35+
if (fragment instanceof MainFragment) {
36+
mMainFragment = (MainFragment) fragment;
37+
}
3438
if (mMainFragment == null || !mMainFragment.isAdded() || !mMainFragment.deselectIfSelected()) {
3539
super.onBackPressed();
3640
}

library/.gitignore

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
16
/build
2-
### JetBrains template
3-
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
4-
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
5-
6-
# User-specific stuff:
7-
.idea/workspace.xml
8-
.idea/tasks.xml
9-
.idea/dictionaries
10-
.idea/vcs.xml
11-
.idea/jsLibraryMappings.xml
12-
13-
# Sensitive or high-churn files:
14-
.idea/dataSources.ids
15-
.idea/dataSources.xml
16-
.idea/dataSources.local.xml
17-
.idea/sqlDataSources.xml
18-
.idea/dynamic.xml
19-
.idea/uiDesigner.xml
20-
21-
# Gradle:
22-
.idea/gradle.xml
23-
.idea/libraries
24-
25-
# Mongo Explorer plugin:
26-
.idea/mongoSettings.xml
27-
28-
## File-based project format:
29-
*.iws
30-
31-
## Plugin-specific files:
32-
33-
# IntelliJ
34-
/out/
35-
36-
# mpeltonen/sbt-idea plugin
37-
.idea_modules/
38-
39-
# JIRA plugin
40-
atlassian-ide-plugin.xml
41-
42-
# Crashlytics plugin (for Android Studio and IntelliJ)
43-
com_crashlytics_export_strings.xml
44-
crashlytics.properties
45-
crashlytics-build.properties
46-
fabric.properties
47-
### Android template
48-
# Built application files
7+
/captures
8+
/.idea
499
*.apk
50-
*.ap_
51-
52-
# Files for the ART/Dalvik VM
53-
*.dex
54-
55-
# Java class files
56-
*.class
57-
58-
# Generated files
59-
bin/
60-
gen/
61-
out/
62-
63-
# Gradle files
64-
.gradle/
65-
build/
66-
67-
# Local configuration file (sdk path, etc)
68-
local.properties
69-
70-
# Proguard folder generated by Eclipse
71-
proguard/
72-
73-
# Log Files
74-
*.log
75-
76-
# Android Studio Navigation editor temp files
77-
.navigation/
78-
79-
# Android Studio captures folder
80-
captures/
81-
82-
# Intellij
83-
*.iml
84-
85-
# Keystore files
86-
*.jks

library/src/main/java/com/cleveroad/fanlayoutmanager/FanLayoutManager.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public class FanLayoutManager extends RecyclerView.LayoutManager {
5050
*/
5151
private final FanLayoutManagerSettings mSettings;
5252

53-
/**
54-
* Map with view (card) rotations. This need to save bounce rotations for views.
55-
* {@link #updateArcViewPositions}
56-
*/
57-
private final SparseArray<Float> mViewRotationsMap = new SparseArray<>();
5853
/**
5954
* Map with view cache.
6055
*/
@@ -73,6 +68,12 @@ public class FanLayoutManager extends RecyclerView.LayoutManager {
7368
*/
7469
private final Random mRandom = new Random();
7570

71+
/**
72+
* Map with view (card) rotations. This need to save bounce rotations for views.
73+
* {@link #updateArcViewPositions}
74+
*/
75+
private SparseArray<Float> mViewRotationsMap = new SparseArray<>();
76+
7677
/**
7778
* Helper module need to implement 'open','close', 'shift' views functionality.
7879
* By default using {@link AnimationHelperImpl}
@@ -145,6 +146,7 @@ public class FanLayoutManager extends RecyclerView.LayoutManager {
145146

146147
public FanLayoutManager(@NonNull Context context) {
147148
this(context, null);
149+
mAnimationHelper = new AnimationHelperImpl();
148150
}
149151

150152
public FanLayoutManager(@NonNull Context context, @Nullable FanLayoutManagerSettings settings) {
@@ -175,7 +177,7 @@ public void saveState() {
175177
// save collapsed state for views
176178
mPendingSavedState.isCollapsed = mIsCollapsed;
177179
// center view position
178-
mScrollToPosition = mPendingSavedState.mCenterItemPosition;
180+
mPendingSavedState.mRotation = mViewRotationsMap;
179181
}
180182

181183
@Override
@@ -184,6 +186,8 @@ public Parcelable onSaveInstanceState() {
184186
return mPendingSavedState;
185187
}
186188

189+
// I'm sure in all casts
190+
@SuppressWarnings("unchecked")
187191
@Override
188192
public void onRestoreInstanceState(Parcelable state) {
189193
if (state != null && state instanceof FanLayoutManager.SavedState) {
@@ -196,6 +200,8 @@ public void onRestoreInstanceState(Parcelable state) {
196200
mIsSelected = mPendingSavedState.isSelected;
197201
// collapsed state
198202
mIsCollapsed = mPendingSavedState.isCollapsed;
203+
// rotation state
204+
mViewRotationsMap = mPendingSavedState.mRotation;
199205
}
200206
}
201207

@@ -325,12 +331,18 @@ public boolean canScrollHorizontally() {
325331

326332
@Override
327333
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
328-
329334
// after fillRightFromCenter(...) we don't need this param.
330335
mScrollToPosition = RecyclerView.NO_POSITION;
331-
// after fillRightFromCenter(...) we don't need this param.
336+
// // after fillRightFromCenter(...) we don't need this param.
332337
mPendingSavedState = null;
333338

339+
if (dx == RecyclerView.NO_POSITION) {
340+
int delta = scrollHorizontallyInternal(dx);
341+
offsetChildrenHorizontal(-delta);
342+
fill(recycler);
343+
return delta;
344+
}
345+
334346
if (mSelectedItemPosition != RecyclerView.NO_POSITION && !mIsSelectAnimationInProcess && !mIsDeselectAnimationInProcess &&
335347
!mIsWaitingToDeselectAnimation && !mIsWaitingToSelectAnimation) {
336348
// if item selected and any animation isn't in progress
@@ -340,7 +352,6 @@ public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, Recycler
340352
if (mIsDeselectAnimationInProcess || mIsSelectAnimationInProcess || mIsViewCollapsing) {
341353
return 0;
342354
}
343-
//
344355
int delta = scrollHorizontallyInternal(dx);
345356
offsetChildrenHorizontal(-delta);
346357
fill(recycler);
@@ -662,7 +673,6 @@ public boolean isItemSelected() {
662673

663674

664675
private void selectItem(final int position, int delay) {
665-
666676
if (mSelectedItemPosition == position) {
667677
// if select already selected item
668678
deselectItem(mSelectedItemPosition);
@@ -789,6 +799,8 @@ public void onAnimationEnd(Animator animator) {
789799
*/
790800

791801
private void closeItem(final RecyclerView recyclerView, final int position, final int scrollToPosition, final int delay) {
802+
// wait for start deselect animation
803+
mIsWaitingToDeselectAnimation = true;
792804
// search view by position
793805
View viewToDeselect = null;
794806
for (int count = getChildCount(), i = 0; i < count; i++) {
@@ -809,9 +821,6 @@ private void closeItem(final RecyclerView recyclerView, final int position, fina
809821
return;
810822
}
811823

812-
// wait for start deselect animation
813-
mIsWaitingToDeselectAnimation = true;
814-
815824
// close item animation
816825
mAnimationHelper.closeItem(viewToDeselect, delay, new SimpleAnimatorListener() {
817826
@Override
@@ -1014,6 +1023,8 @@ public void restoreBaseRotationSelectedItem(final Animator.AnimatorListener list
10141023
}
10151024

10161025
if (viewToRotate != null) {
1026+
// save state
1027+
mIsSelectedItemStraightenedInProcess = true;
10171028

10181029
// start straight animation
10191030
mAnimationHelper.rotateView(viewToRotate, baseViewRotation, new Animator.AnimatorListener() {
@@ -1050,9 +1061,6 @@ public void onAnimationRepeat(Animator animation) {
10501061
}
10511062
}
10521063
});
1053-
1054-
// save state
1055-
mIsSelectedItemStraightenedInProcess = true;
10561064
}
10571065
}
10581066

@@ -1225,6 +1233,7 @@ public FanLayoutManager.SavedState[] newArray(int size) {
12251233
int mCenterItemPosition = RecyclerView.NO_POSITION;
12261234
boolean isCollapsed;
12271235
boolean isSelected;
1236+
SparseArray mRotation;
12281237

12291238
public SavedState() {
12301239

@@ -1234,24 +1243,28 @@ public SavedState() {
12341243
mCenterItemPosition = in.readInt();
12351244
isCollapsed = in.readInt() == 1;
12361245
isSelected = in.readInt() == 1;
1246+
mRotation = in.readSparseArray(getClass().getClassLoader());
12371247
}
12381248

12391249
public SavedState(FanLayoutManager.SavedState other) {
12401250
mCenterItemPosition = other.mCenterItemPosition;
12411251
isCollapsed = other.isCollapsed;
12421252
isSelected = other.isSelected;
1253+
mRotation = other.mRotation;
12431254
}
12441255

12451256
@Override
12461257
public int describeContents() {
12471258
return 0;
12481259
}
12491260

1261+
@SuppressWarnings("unchecked")
12501262
@Override
12511263
public void writeToParcel(Parcel dest, int flags) {
12521264
dest.writeInt(mCenterItemPosition);
12531265
dest.writeInt(isCollapsed ? 1 : 0);
12541266
dest.writeInt(isSelected ? 1 : 0);
1267+
dest.writeSparseArray(mRotation);
12551268
}
12561269
}
12571270

0 commit comments

Comments
 (0)