|
| 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 | +package com.google.androidstudio.motionlayoutexample.youtubedemo |
| 18 | + |
| 19 | +import android.support.v7.widget.RecyclerView |
| 20 | +import android.view.LayoutInflater |
| 21 | +import android.view.View |
| 22 | +import android.view.ViewGroup |
| 23 | +import android.widget.ImageView |
| 24 | +import android.widget.TextView |
| 25 | +import com.bumptech.glide.Glide |
| 26 | +import com.google.androidstudio.motionlayoutexample.youtubedemo.YouTubeDemoViewHolder.CatRowViewHolder |
| 27 | +import com.google.androidstudio.motionlayoutexample.youtubedemo.YouTubeDemoViewHolder.TextDescriptionViewHolder |
| 28 | +import com.google.androidstudio.motionlayoutexample.youtubedemo.YouTubeDemoViewHolder.TextHeaderViewHolder |
| 29 | +import com.google.androidstudio.motionlayoutexample.R |
| 30 | + |
| 31 | +class FrontPhotosAdapter : RecyclerView.Adapter<YouTubeDemoViewHolder>() { |
| 32 | + |
| 33 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YouTubeDemoViewHolder { |
| 34 | + val inflater = LayoutInflater.from(parent.context) |
| 35 | + val itemView = inflater.inflate(viewType, parent, false) |
| 36 | + return when (viewType) { |
| 37 | + R.layout.motion_24_recyclerview_expanded_text_header -> TextHeaderViewHolder (itemView) |
| 38 | + R.layout.motion_24_recyclerview_expanded_text_description -> TextDescriptionViewHolder (itemView) |
| 39 | + R.layout.motion_24_recyclerview_expanded_row -> CatRowViewHolder(itemView) |
| 40 | + else -> throw IllegalStateException("Unknown viewType $viewType") |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + override fun onBindViewHolder(holder: YouTubeDemoViewHolder, position: Int) { |
| 45 | + when (holder) { |
| 46 | + is TextHeaderViewHolder -> {} |
| 47 | + is TextDescriptionViewHolder -> {} |
| 48 | + is CatRowViewHolder -> { |
| 49 | + val imagePosition = position - 2 |
| 50 | + holder.textView.text = holder.textView.resources.getString(R.string.cat_n, imagePosition) |
| 51 | + Glide.with(holder.imageView) |
| 52 | + .load(Cats.catImages[imagePosition]) |
| 53 | + .into(holder.imageView) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override fun getItemViewType(position: Int): Int { |
| 59 | + |
| 60 | + return when (position) { |
| 61 | + 0 -> R.layout.motion_24_recyclerview_expanded_text_header |
| 62 | + 1 -> R.layout.motion_24_recyclerview_expanded_text_description |
| 63 | + else -> R.layout.motion_24_recyclerview_expanded_row |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override fun getItemCount() = Cats.catImages.size + 2 // For text header and text description |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * [RecyclerView.ViewHolder] types used by this adapter. |
| 72 | + */ |
| 73 | +sealed class YouTubeDemoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| 74 | + |
| 75 | + class TextHeaderViewHolder( |
| 76 | + itemView: View |
| 77 | + ) : YouTubeDemoViewHolder(itemView) |
| 78 | + |
| 79 | + class TextDescriptionViewHolder( |
| 80 | + itemView: View |
| 81 | + ) : YouTubeDemoViewHolder(itemView) |
| 82 | + |
| 83 | + class CatRowViewHolder( |
| 84 | + itemView: View |
| 85 | + ) : YouTubeDemoViewHolder(itemView) { |
| 86 | + val imageView = itemView.findViewById(R.id.image_row) as ImageView |
| 87 | + val textView = itemView.findViewById(R.id.text_row) as TextView |
| 88 | + } |
| 89 | +} |
0 commit comments