1
+ /*
2
+ * Copyright 2023-2025 wintmain
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .wintmain .wBasis .transition ;
18
+
19
+ import android .os .Bundle ;
20
+ import android .transition .TransitionInflater ;
21
+ import android .view .LayoutInflater ;
22
+ import android .view .View ;
23
+ import android .view .View .OnLayoutChangeListener ;
24
+ import android .view .ViewGroup ;
25
+ import androidx .annotation .NonNull ;
26
+ import androidx .annotation .Nullable ;
27
+ import androidx .core .app .SharedElementCallback ;
28
+ import androidx .fragment .app .Fragment ;
29
+ import androidx .recyclerview .widget .RecyclerView ;
30
+ import com .wintmain .wBasis .R ;
31
+ import com .wintmain .wBasis .transition .adapter .GridAdapter ;
32
+
33
+ import java .util .List ;
34
+ import java .util .Map ;
35
+
36
+ /**
37
+ * A fragment for displaying a grid of images.
38
+ */
39
+ public class GridToPagerFragment extends Fragment {
40
+
41
+ private RecyclerView recyclerView ;
42
+
43
+ @ Nullable
44
+ @ Override
45
+ public View onCreateView (@ NonNull LayoutInflater inflater , @ Nullable ViewGroup container ,
46
+ @ Nullable Bundle savedInstanceState ) {
47
+ recyclerView = (RecyclerView ) inflater .inflate (R .layout .fragment_grid_to_pager , container ,
48
+ false );
49
+ recyclerView .setAdapter (new GridAdapter (this ));
50
+
51
+ prepareTransitions ();
52
+ postponeEnterTransition ();
53
+
54
+ return recyclerView ;
55
+ }
56
+
57
+ @ Override
58
+ public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
59
+ super .onViewCreated (view , savedInstanceState );
60
+ scrollToPosition ();
61
+ }
62
+
63
+ /**
64
+ * Scrolls the recycler view to show the last viewed item in the grid. This is important when
65
+ * navigating back from the grid.
66
+ */
67
+ private void scrollToPosition () {
68
+ recyclerView .addOnLayoutChangeListener (new OnLayoutChangeListener () {
69
+ @ Override
70
+ public void onLayoutChange (View v ,
71
+ int left ,
72
+ int top ,
73
+ int right ,
74
+ int bottom ,
75
+ int oldLeft ,
76
+ int oldTop ,
77
+ int oldRight ,
78
+ int oldBottom ) {
79
+ recyclerView .removeOnLayoutChangeListener (this );
80
+ final RecyclerView .LayoutManager layoutManager = recyclerView .getLayoutManager ();
81
+ View viewAtPosition = layoutManager .findViewByPosition (
82
+ GridToPagerMainActivity .currentPosition );
83
+ // Scroll to position if the view for the current position is null (not currently
84
+ // part of
85
+ // layout manager children), or it's not completely visible.
86
+ if (viewAtPosition == null || layoutManager
87
+ .isViewPartiallyVisible (viewAtPosition , false , true )) {
88
+ recyclerView .post (() -> layoutManager .scrollToPosition (
89
+ GridToPagerMainActivity .currentPosition ));
90
+ }
91
+ }
92
+ });
93
+ }
94
+
95
+ /**
96
+ * Prepares the shared element transition to the pager fragment, as well as the other
97
+ * transitions
98
+ * that affect the flow.
99
+ */
100
+ private void prepareTransitions () {
101
+ setExitTransition (TransitionInflater .from (getContext ())
102
+ .inflateTransition (R .transition .grid_exit_transition ));
103
+
104
+ // A similar mapping is set at the ImagePagerFragment with a setEnterSharedElementCallback.
105
+ setExitSharedElementCallback (
106
+ new SharedElementCallback () {
107
+ @ Override
108
+ public void onMapSharedElements (List <String > names ,
109
+ Map <String , View > sharedElements ) {
110
+ // Locate the ViewHolder for the clicked position.
111
+ RecyclerView .ViewHolder selectedViewHolder = recyclerView
112
+ .findViewHolderForAdapterPosition (
113
+ GridToPagerMainActivity .currentPosition );
114
+ if (selectedViewHolder == null ) {
115
+ return ;
116
+ }
117
+
118
+ // Map the first shared element name to the child ImageView.
119
+ sharedElements
120
+ .put (names .get (0 ),
121
+ selectedViewHolder .itemView .findViewById (R .id .card_image ));
122
+ }
123
+ });
124
+ }
125
+ }
0 commit comments