1 /* 2 * Copyright (C) 2019 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.android.car.themeplayground; 18 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.widget.TextView; 23 24 import androidx.annotation.NonNull; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import java.util.ArrayList; 28 29 /** 30 * Implementation of {@link RecyclerViewAdapter} that can be used with RecyclerViews. 31 */ 32 public class RecyclerViewAdapter extends 33 RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> { 34 35 private ArrayList<String> mData; 36 RecyclerViewAdapter(ArrayList<String> data)37 RecyclerViewAdapter(ArrayList<String> data) { 38 this.mData = data; 39 } 40 41 @NonNull 42 @Override onCreateViewHolder(@onNull ViewGroup parent, int viewType)43 public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 44 LayoutInflater inflator = LayoutInflater.from(parent.getContext()); 45 View view = inflator.inflate(R.layout.item_list, parent, false); 46 return new RecyclerViewHolder(view); 47 } 48 49 @Override onBindViewHolder(@onNull RecyclerViewHolder holder, int position)50 public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) { 51 String title = mData.get(position); 52 holder.mTextTitle.setText(title); 53 } 54 55 @Override getItemCount()56 public int getItemCount() { 57 return mData.size(); 58 } 59 60 61 /** 62 * Holds views for each element in the list. 63 */ 64 public static class RecyclerViewHolder extends RecyclerView.ViewHolder { 65 TextView mTextTitle; 66 RecyclerViewHolder(@onNull View itemView)67 RecyclerViewHolder(@NonNull View itemView) { 68 super(itemView); 69 mTextTitle = itemView.findViewById(R.id.textTitle); 70 } 71 } 72 } 73