blob: f90425b4f92380de911151afb9157bd4f5afb95f [file] [log] [blame]
Florina Muntenescuad8d1a62018-09-21 16:21:40 +01001/*
Jakub Gielzakaea73642019-01-25 15:04:36 +00002 * Copyright 2018 The Android Open Source Project
Florina Muntenescuad8d1a62018-09-21 16:21:40 +01003 *
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 * http://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
17package com.example.androidx.viewpager2
18
19import android.os.Bundle
20import android.view.View
21import android.widget.AdapterView
22import android.widget.ArrayAdapter
23import android.widget.Button
24import android.widget.CheckBox
25import android.widget.Spinner
26import android.widget.SpinnerAdapter
27import androidx.fragment.app.FragmentActivity
28import androidx.viewpager2.widget.ViewPager2
Jakub Gielzakb60fc002018-10-19 16:36:59 +010029import androidx.viewpager2.widget.ViewPager2.ORIENTATION_HORIZONTAL
30import androidx.viewpager2.widget.ViewPager2.ORIENTATION_VERTICAL
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010031import com.example.androidx.viewpager2.cards.Card
32
33/**
34 * Base class for the two activities in the demo. Sets up the list of cards and implements UI to
35 * jump to arbitrary cards using setCurrentItem, either with or without smooth scrolling.
36 */
37abstract class BaseCardActivity : FragmentActivity() {
38
39 lateinit var viewPager: ViewPager2
Jelle Fresen245f8ec2018-12-17 16:21:41 +000040 private lateinit var cardSelector: Spinner
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010041 private lateinit var smoothScrollCheckBox: CheckBox
42 private lateinit var rotateCheckBox: CheckBox
43 private lateinit var translateCheckBox: CheckBox
44 private lateinit var scaleCheckBox: CheckBox
45 private lateinit var gotoPage: Button
46 private lateinit var orientationSelector: Spinner
Jelle Fresen87099db2019-01-25 16:10:58 +000047 private lateinit var disableUserInputCheckBox: CheckBox
Jakub Gielzakb60fc002018-10-19 16:36:59 +010048 private var orientation: Int = ORIENTATION_HORIZONTAL
Jelle Fresenb82b8272018-10-12 16:18:19 +010049
Jakub Gielzakb60fc002018-10-19 16:36:59 +010050 private val translateX get() = orientation == ORIENTATION_VERTICAL &&
Jelle Fresenb82b8272018-10-12 16:18:19 +010051 translateCheckBox.isChecked
Jakub Gielzakb60fc002018-10-19 16:36:59 +010052 private val translateY get() = orientation == ORIENTATION_HORIZONTAL &&
Jelle Fresenb82b8272018-10-12 16:18:19 +010053 translateCheckBox.isChecked
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010054
Jelle Fresen53d5dd82018-12-17 16:46:52 +000055 protected open val layoutId: Int = R.layout.activity_no_tablayout
Jelle Fresen245f8ec2018-12-17 16:21:41 +000056
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010057 private val mAnimator = ViewPager2.PageTransformer { page, position ->
58 val absPos = Math.abs(position)
59 page.apply {
Jelle Fresenb82b8272018-10-12 16:18:19 +010060 rotation = if (rotateCheckBox.isChecked) position * 360 else 0f
61 translationY = if (translateY) absPos * 500f else 0f
62 translationX = if (translateX) absPos * 350f else 0f
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010063 if (scaleCheckBox.isChecked) {
Jelle Fresenb82b8272018-10-12 16:18:19 +010064 val scale = if (absPos > 1) 0F else 1 - absPos
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010065 scaleX = scale
66 scaleY = scale
67 } else {
68 scaleX = 1f
69 scaleY = 1f
70 }
71 }
72 }
73
Jelle Fresene280e942018-10-17 12:56:29 +010074 public override fun onCreate(savedInstanceState: Bundle?) {
75 super.onCreate(savedInstanceState)
Jelle Fresen245f8ec2018-12-17 16:21:41 +000076 setContentView(layoutId)
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010077
78 viewPager = findViewById(R.id.view_pager)
79 orientationSelector = findViewById(R.id.orientation_spinner)
Jelle Fresen87099db2019-01-25 16:10:58 +000080 disableUserInputCheckBox = findViewById(R.id.disable_user_input_checkbox)
Jelle Fresen245f8ec2018-12-17 16:21:41 +000081 cardSelector = findViewById(R.id.card_spinner)
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010082 smoothScrollCheckBox = findViewById(R.id.smooth_scroll_checkbox)
83 rotateCheckBox = findViewById(R.id.rotate_checkbox)
84 translateCheckBox = findViewById(R.id.translate_checkbox)
85 scaleCheckBox = findViewById(R.id.scale_checkbox)
86 gotoPage = findViewById(R.id.jump_button)
87
Jelle Fresen87099db2019-01-25 16:10:58 +000088 disableUserInputCheckBox.setOnCheckedChangeListener { _, isDisabled ->
89 viewPager.isUserInputEnabled = !isDisabled
90 }
91
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010092 orientationSelector.adapter = createOrientationAdapter()
Jelle Fresen245f8ec2018-12-17 16:21:41 +000093 cardSelector.adapter = createCardAdapter()
Florina Muntenescuad8d1a62018-09-21 16:21:40 +010094
95 viewPager.setPageTransformer(mAnimator)
96
97 orientationSelector.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
98 override fun onItemSelected(
99 parent: AdapterView<*>,
Jelle Fresen5d176f62018-10-12 16:44:53 +0100100 view: View?,
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100101 position: Int,
102 id: Long
103 ) {
104 when (parent.selectedItem.toString()) {
Jakub Gielzakb60fc002018-10-19 16:36:59 +0100105 HORIZONTAL -> orientation = ORIENTATION_HORIZONTAL
106 VERTICAL -> orientation = ORIENTATION_VERTICAL
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100107 }
Jelle Fresenb82b8272018-10-12 16:18:19 +0100108 viewPager.orientation = orientation
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100109 }
110
111 override fun onNothingSelected(adapterView: AdapterView<*>) {}
112 }
113
114 gotoPage.setOnClickListener {
Jelle Fresen245f8ec2018-12-17 16:21:41 +0000115 val card = cardSelector.selectedItemPosition
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100116 val smoothScroll = smoothScrollCheckBox.isChecked
Jelle Fresen245f8ec2018-12-17 16:21:41 +0000117 viewPager.setCurrentItem(card, smoothScroll)
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100118 }
119 }
120
Jelle Fresen245f8ec2018-12-17 16:21:41 +0000121 private fun createCardAdapter(): SpinnerAdapter {
122 val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, Card.DECK)
123 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
124 return adapter
125 }
126
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100127 private fun createOrientationAdapter(): SpinnerAdapter {
128 val adapter = ArrayAdapter(this,
129 android.R.layout.simple_spinner_item, arrayOf(HORIZONTAL, VERTICAL))
130 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
131 return adapter
132 }
133
134 companion object {
Jelle Fresen245f8ec2018-12-17 16:21:41 +0000135 val cards = Card.DECK
Florina Muntenescuad8d1a62018-09-21 16:21:40 +0100136 private const val HORIZONTAL = "horizontal"
137 private const val VERTICAL = "vertical"
138 }
139}