Skip to content

Commit f466508

Browse files
add external grid to ViewPager tutorial
1 parent 3859ae9 commit f466508

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1178
-6
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2018 Google LLC
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+
plugins {
18+
id 'com.android.application'
19+
id 'kotlin-android'
20+
id 'kotlin-android-extensions'
21+
id 'kotlin-kapt'
22+
}
23+
24+
android {
25+
compileSdkVersion 30
26+
buildToolsVersion "30.0.2"
27+
28+
defaultConfig {
29+
applicationId "com.google.samples.gridtopager"
30+
minSdkVersion 21
31+
targetSdkVersion 30
32+
versionCode 1
33+
versionName "1.0"
34+
testInstrumentationRunner = 'androidx.test.runner.AndroidJUnitRunner'
35+
}
36+
buildTypes {
37+
release {
38+
minifyEnabled false
39+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
40+
}
41+
}
42+
compileOptions {
43+
sourceCompatibility JavaVersion.VERSION_1_8
44+
targetCompatibility JavaVersion.VERSION_1_8
45+
}
46+
}
47+
48+
dependencies {
49+
implementation "androidx.appcompat:appcompat:1.2.0"
50+
implementation "androidx.cardview:cardview:1.0.0"
51+
implementation "androidx.constraintlayout:constraintlayout:2.0.2"
52+
implementation "androidx.recyclerview:recyclerview:1.1.0"
53+
implementation "androidx.annotation:annotation:1.1.0"
54+
implementation "com.github.bumptech.glide:glide:4.11.0"
55+
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
56+
androidTestImplementation "androidx.test:rules:1.3.0"
57+
androidTestImplementation "androidx.test.ext:junit:1.1.2"
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/google.samples/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
package com.google.samples.gridtopager
17+
18+
import androidx.test.espresso.Espresso.onView
19+
import androidx.test.espresso.assertion.ViewAssertions.matches
20+
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
21+
import androidx.test.espresso.matcher.ViewMatchers.withId
22+
import androidx.test.filters.LargeTest
23+
import androidx.test.rule.ActivityTestRule
24+
import org.junit.Rule
25+
import org.junit.Test
26+
27+
@LargeTest
28+
class MainActivityTest {
29+
30+
@Rule
31+
@JvmField
32+
var mActivityTestRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
33+
34+
@Test
35+
fun recyclerView_isDisplayed() {
36+
onView(withId(R.id.recycler_view)).check(matches(isDisplayed()))
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2018 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.google.samples.gridtopager">
20+
21+
<application
22+
android:allowBackup="true"
23+
android:icon="@mipmap/ic_launcher"
24+
android:label="@string/app_name"
25+
android:roundIcon="@mipmap/ic_launcher_round"
26+
android:supportsRtl="true"
27+
android:theme="@style/AppTheme">
28+
<activity android:name="com.google.samples.gridtopager.MainActivity">
29+
<intent-filter>
30+
<action android:name="android.intent.action.MAIN"/>
31+
<category android:name="android.intent.category.LAUNCHER"/>
32+
</intent-filter>
33+
</activity>
34+
</application>
35+
36+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2018 Google LLC
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+
package com.google.samples.gridtopager;
17+
18+
import android.os.Bundle;
19+
20+
import androidx.annotation.NonNull;
21+
import androidx.fragment.app.FragmentManager;
22+
import androidx.appcompat.app.AppCompatActivity;
23+
import com.google.samples.gridtopager.fragment.GridFragment;
24+
25+
/**
26+
* Grid to pager app's main activity.
27+
*/
28+
public class MainActivity extends AppCompatActivity {
29+
30+
/**
31+
* Holds the current image position to be shared between the grid and the pager fragments. This
32+
* position updated when a grid item is clicked, or when paging the pager.
33+
*
34+
* In this demo app, the position always points to an image index at the {@link
35+
* com.google.samples.gridtopager.adapter.ImageData} class.
36+
*/
37+
public static int currentPosition;
38+
private static final String KEY_CURRENT_POSITION = "com.google.samples.gridtopager.key.currentPosition";
39+
40+
@Override
41+
protected void onCreate(Bundle savedInstanceState) {
42+
super.onCreate(savedInstanceState);
43+
setContentView(R.layout.activity_main);
44+
if (savedInstanceState != null) {
45+
currentPosition = savedInstanceState.getInt(KEY_CURRENT_POSITION, 0);
46+
// Return here to prevent adding additional GridFragments when changing orientation.
47+
return;
48+
}
49+
FragmentManager fragmentManager = getSupportFragmentManager();
50+
fragmentManager
51+
.beginTransaction()
52+
.add(R.id.fragment_container, new GridFragment(), GridFragment.class.getSimpleName())
53+
.commit();
54+
}
55+
56+
@Override
57+
protected void onSaveInstanceState(@NonNull Bundle outState) {
58+
super.onSaveInstanceState(outState);
59+
outState.putInt(KEY_CURRENT_POSITION, currentPosition);
60+
}
61+
}

0 commit comments

Comments
 (0)