1/*
2 * Copyright (C) 2014 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.supportv7.widget.adapter;
18
19import android.content.Context;
20import android.graphics.Color;
21import android.util.TypedValue;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25import androidx.recyclerview.widget.RecyclerView;
26
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30
31/**
32 * A simple RecyclerView adapter that displays every string passed in a constructor as an item.
33 */
34public class SimpleStringAdapter extends RecyclerView.Adapter<SimpleStringAdapter.ViewHolder> {
35
36    private int mBackground;
37
38    private List<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 SimpleStringAdapter(Context context, String[] strings) {
60        TypedValue val = new TypedValue();
61        if (context.getTheme() != null) {
62            context.getTheme().resolveAttribute(
63                    android.R.attr.selectableItemBackground, val, true);
64        }
65        mBackground = val.resourceId;
66        mValues = new ArrayList<String>();
67        Collections.addAll(mValues, strings);
68    }
69
70    public void swap(int pos1, int pos2) {
71        String tmp = mValues.get(pos1);
72        mValues.set(pos1, mValues.get(pos2));
73        mValues.set(pos2, tmp);
74        notifyItemRemoved(pos1);
75        notifyItemInserted(pos2);
76    }
77
78    @Override
79    public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
80        final ViewHolder h = new ViewHolder(new TextView(parent.getContext()));
81        h.mTextView.setMinimumHeight(128);
82        h.mTextView.setPadding(20, 0, 20, 0);
83        h.mTextView.setFocusable(true);
84        h.mTextView.setBackgroundResource(mBackground);
85        RecyclerView.LayoutParams lp = getLayoutParams();
86        h.mTextView.setLayoutParams(lp);
87        return h;
88    }
89
90    @Override
91    public void onBindViewHolder(ViewHolder holder, int position) {
92        holder.mBoundString = mValues.get(position);
93        holder.mTextView.setText(position + ":" + mValues.get(position));
94        holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10));
95        holder.mTextView.setBackgroundColor(getBackgroundColor(position));
96    }
97
98
99    /**
100     * Returns LayoutParams to be used for each item in this adapter. It can be overridden
101     * to provide different LayoutParams.
102     * @return LayoutParams to be used for each item in this adapter.
103     */
104    public RecyclerView.LayoutParams getLayoutParams() {
105        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
106                ViewGroup.LayoutParams.WRAP_CONTENT,
107                ViewGroup.LayoutParams.WRAP_CONTENT);
108        lp.leftMargin = 10;
109        lp.rightMargin = 5;
110        lp.topMargin = 20;
111        lp.bottomMargin = 15;
112        return lp;
113    }
114
115    private int getBackgroundColor(int position) {
116        switch (position % 4) {
117            case 0: return Color.BLACK;
118            case 1: return Color.RED;
119            case 2: return Color.DKGRAY;
120            case 3: return Color.BLUE;
121        }
122        return Color.TRANSPARENT;
123    }
124
125    @Override
126    public int getItemCount() {
127        return mValues.size();
128    }
129
130    public List<String> getValues() {
131        return mValues;
132    }
133
134    public void setValues(List<String> values) {
135        mValues = values;
136    }
137}
138