1 /*
2  * Copyright (C) 2020 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 package com.android.car.rotaryplayground;
17 
18 import android.content.Context;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.TextView;
23 
24 import androidx.recyclerview.widget.RecyclerView;
25 
26 import java.util.Collections;
27 import java.util.List;
28 
29 /**
30  * The adapter that populates the example rotary grid view with strings.
31  */
32 final class RotaryGridAdapter extends RecyclerView.Adapter<RotaryGridAdapter.ItemViewHolder> {
33 
34     private final String[] mData;
35     private final Context mContext;
36     private final LayoutInflater mInflater;
37 
RotaryGridAdapter(Context context, String[] data)38     RotaryGridAdapter(Context context, String[] data) {
39         this.mContext = context;
40         this.mInflater = LayoutInflater.from(context);
41         this.mData = data;
42     }
43 
44     @Override
onCreateViewHolder(ViewGroup parent, int viewType)45     public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
46         View view = mInflater.inflate(
47                 R.layout.rotary_grid_item, parent, /* attachToRoot= */ false);
48         return new ItemViewHolder(view);
49     }
50 
51     @Override
onBindViewHolder(ItemViewHolder holder, int position)52     public void onBindViewHolder(ItemViewHolder holder, int position) {
53         holder.setText(mData[position]);
54     }
55 
56     @Override
getItemCount()57     public int getItemCount() {
58         return mData.length;
59     }
60 
61     /** The viewholder class that contains the grid item data. */
62     static class ItemViewHolder extends RecyclerView.ViewHolder {
63         private TextView mItemText;
64 
ItemViewHolder(View view)65         ItemViewHolder(View view) {
66             super(view);
67             mItemText = itemView.findViewById(R.id.rotary_grid_item_text);
68         }
69 
setText(String text)70         void setText(String text) {
71             this.mItemText.setText(text);
72         }
73     }
74 }