Skip to content

Commit 0c3c723

Browse files
wintmainwosys
authored andcommitted
[wBasis][feat]Add grid to pager sample
1 parent 844243d commit 0c3c723

20 files changed

+925
-11
lines changed

app-catalog/samples/wBasis/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
implementation libs.androidx.lifecycle.runtime.ktx
5656
implementation libs.androidx.ui.graphics
5757
implementation libs.compose.ui.tooling.preview
58+
implementation libs.glide
5859

5960
implementation 'com.squareup.picasso:picasso:2.4.0'
6061
}

app-catalog/samples/wBasis/src/main/AndroidManifest.xml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
android:label="title_activity_main2"
3838
android:theme="@style/Theme.Android_Kickoff" />
3939

40-
<!-- callnotification START -->
40+
<!-- call notification START -->
4141
<activity
4242
android:name=".telephony.callnotification.CallNotificationSample"
4343
android:exported="true"
@@ -48,21 +48,21 @@
4848
android:name=".telephony.callnotification.CallNotificationSample$NotificationReceiver"
4949
android:exported="false"
5050
tools:targetApi="28" />
51-
<!-- callnotification END-->
51+
<!-- call notification END-->
5252

53-
<!-- networkConnectivity START-->
53+
<!-- network connectivity START-->
5454
<activity
5555
android:name=".telephony.networkconnectivity.NetworkConnActivity"
5656
android:exported="true"
5757
android:theme="@style/Theme.AppCompat.DayNight" />
58-
<!-- networkConnectivity END-->
58+
<!-- network connectivity END-->
5959

60-
<!-- recyclerview START-->
60+
<!-- recycler view START-->
6161
<activity
6262
android:name=".ui.recyclerview.RecyclerViewActivity"
6363
android:exported="true"
6464
android:theme="@style/Theme.AppCompat.DayNight" />
65-
<!-- recyclerview END-->
65+
<!-- recycler view END-->
6666

6767
<!-- gesture detect START-->
6868
<activity
@@ -71,12 +71,12 @@
7171
android:theme="@style/Theme.AppCompat.DayNight" />
7272
<!-- gesture detect END-->
7373

74-
<!-- slidingpanelayout START-->
74+
<!-- sliding panel layout START-->
7575
<activity
7676
android:name=".ui.layout.slidingpanelayout.SlidingPaneLayoutMain"
7777
android:exported="true"
78-
android:theme="@style/Theme.AppCompat.DayNight"></activity>
79-
<!-- slidingpanelayout END-->
78+
android:theme="@style/Theme.AppCompat.DayNight" />
79+
<!-- sliding panel layout END-->
8080

8181
<activity
8282
android:name=".PlaceHolderActivity"
@@ -235,10 +235,8 @@
235235
android:exported="true"
236236
android:theme="@style/Theme.AppCompat.DayNight">
237237
<intent-filter>
238-
239238
<!-- <action android:name="android.intent.action.VIEW"/> -->
240239
<action android:name="test_action" />
241-
242240
<category android:name="android.intent.category.DEFAULT" />
243241
</intent-filter>
244242
</activity>
@@ -356,6 +354,7 @@
356354
android:name=".aidl.PeopleRemoteService"
357355
android:enabled="true"
358356
android:exported="true"
357+
android:permission="TODO"
359358
android:process=":remote" />
360359

361360
<activity
@@ -387,6 +386,11 @@
387386
android:name=".transition.SceneTransitionBasicDetailActivity"
388387
android:exported="true"
389388
android:theme="@style/Scene_Activity_AppTheme" />
389+
390+
<activity
391+
android:name=".transition.GridToPagerMainActivity"
392+
android:exported="true"
393+
android:theme="@style/GridToPager_Activity_Theme" />
390394
</application>
391395

392396
</manifest>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 androidx.annotation.NonNull;
21+
import androidx.appcompat.app.AppCompatActivity;
22+
import androidx.fragment.app.FragmentManager;
23+
import com.google.android.catalog.framework.annotations.Sample;
24+
import com.wintmain.wBasis.R;
25+
26+
/**
27+
* Grid to pager app's main activity.
28+
*/
29+
@Sample(name = "Grid to pager",
30+
description = "viewpager & glide 照片展示",
31+
tags = {"android-samples", "animation-samples"}
32+
)
33+
public class GridToPagerMainActivity extends AppCompatActivity {
34+
35+
private static final String KEY_CURRENT_POSITION =
36+
"com.wintmain.wBasis.gridtopager.key.currentPosition";
37+
/**
38+
* Holds the current image position to be shared between the grid and the pager fragments. This
39+
* position updated when a grid item is clicked, or when paging the pager.
40+
*
41+
* In this demo app, the position always points to an image index at the {@link
42+
* com.wintmain.wBasis.transition.util.ImageData} class.
43+
*/
44+
public static int currentPosition;
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
super.onCreate(savedInstanceState);
49+
setContentView(R.layout.activity_grid_to_pager_main);
50+
if (savedInstanceState != null) {
51+
currentPosition = savedInstanceState.getInt(KEY_CURRENT_POSITION, 0);
52+
// Return here to prevent adding additional GridFragments when changing orientation.
53+
return;
54+
}
55+
FragmentManager fragmentManager = getSupportFragmentManager();
56+
fragmentManager
57+
.beginTransaction()
58+
.add(R.id.fragment_container, new GridToPagerFragment(),
59+
GridToPagerFragment.class.getSimpleName())
60+
.commit();
61+
}
62+
63+
@Override
64+
protected void onSaveInstanceState(@NonNull Bundle outState) {
65+
super.onSaveInstanceState(outState);
66+
outState.putInt(KEY_CURRENT_POSITION, currentPosition);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)