Home | History | Annotate | Download | only in cardview
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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  *      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 
     17 package com.example.android.cardview
     18 
     19 import android.app.Fragment
     20 import android.os.Bundle
     21 import android.support.annotation.VisibleForTesting
     22 import android.support.v7.widget.CardView
     23 import android.util.Log
     24 import android.view.LayoutInflater
     25 import android.view.View
     26 import android.view.ViewGroup
     27 import android.widget.SeekBar
     28 
     29 /**
     30  * Fragment that demonstrates how to use [CardView].
     31  */
     32 class CardViewFragment : Fragment() {
     33 
     34     private val TAG = "CardViewFragment"
     35 
     36     // The [CardView] widget.
     37     @VisibleForTesting lateinit var cardView: CardView
     38 
     39     // SeekBar that changes the cornerRadius attribute for the cardView widget.
     40     @VisibleForTesting lateinit var radiusSeekBar: SeekBar
     41 
     42     // SeekBar that changes the Elevation attribute for the cardView widget.
     43     @VisibleForTesting lateinit var elevationSeekBar: SeekBar
     44 
     45     override fun onCreateView(
     46         inflater: LayoutInflater,
     47         container: ViewGroup?,
     48         savedInstanceState: Bundle?
     49     ): View {
     50         return inflater.inflate(R.layout.fragment_card_view, container, false)
     51     }
     52 
     53     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     54         super.onViewCreated(view, savedInstanceState)
     55         cardView = view.findViewById(R.id.cardview)
     56 
     57         radiusSeekBar = view.findViewById(R.id.cardview_radius_seekbar)
     58         radiusSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
     59             override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
     60                 Log.d(TAG, "SeekBar Radius progress: $progress")
     61                 cardView.radius = progress.toFloat()
     62             }
     63 
     64             override fun onStartTrackingTouch(seekBar: SeekBar) = Unit // Do nothing
     65 
     66             override fun onStopTrackingTouch(seekBar: SeekBar) = Unit // Do nothing
     67         })
     68 
     69         elevationSeekBar = view.findViewById(R.id.cardview_elevation_seekbar)
     70         elevationSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
     71             override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
     72                 Log.d(TAG, "SeekBar Elevation progress : $progress")
     73                 cardView.elevation = progress.toFloat()
     74             }
     75 
     76             override fun onStartTrackingTouch(seekBar: SeekBar) = Unit // Do nothing
     77 
     78             override fun onStopTrackingTouch(seekBar: SeekBar) = Unit // Do nothing
     79         })
     80     }
     81 
     82 }
     83