1/* 2 * Copyright (C) 2015 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 17package com.example.android.support.design.widget; 18 19import com.example.android.support.design.R; 20 21import android.content.Context; 22import android.graphics.Color; 23import android.support.v7.widget.RecyclerView; 24import android.text.Layout; 25import android.util.TypedValue; 26import android.view.LayoutInflater; 27import android.view.ViewGroup; 28import android.widget.TextView; 29 30import java.util.ArrayList; 31import java.util.Collections; 32 33public class SimpleStringRecyclerViewAdapter 34 extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> { 35 36 private int mBackground; 37 38 private ArrayList<String> mValues; 39 40 public static class ViewHolder extends RecyclerView.ViewHolder { 41 public String mBoundString; 42 public TextView mTextView; 43 44 public ViewHolder(TextView v) { 45 super(v); 46 mTextView = v; 47 } 48 49 @Override 50 public String toString() { 51 return super.toString() + " '" + mTextView.getText(); 52 } 53 } 54 55 public String getValueAt(int position) { 56 return mValues.get(position); 57 } 58 59 public SimpleStringRecyclerViewAdapter(Context context, String[] strings) { 60 TypedValue val = new TypedValue(); 61 if (context.getTheme() != null) { 62 context.getTheme().resolveAttribute(R.attr.selectableItemBackground, val, true); 63 } 64 mBackground = val.resourceId; 65 mValues = new ArrayList<>(); 66 Collections.addAll(mValues, strings); 67 } 68 69 @Override 70 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 71 TextView textView = (TextView) LayoutInflater.from(parent.getContext()) 72 .inflate(android.R.layout.simple_list_item_1, parent, false); 73 textView.setBackgroundResource(mBackground); 74 return new ViewHolder(textView); 75 } 76 77 @Override 78 public void onBindViewHolder(ViewHolder holder, int position) { 79 holder.mBoundString = mValues.get(position); 80 holder.mTextView.setText(position + ": " + mValues.get(position)); 81 } 82 83 @Override 84 public int getItemCount() { 85 return mValues.size(); 86 } 87} 88