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