1/*
2 * Copyright (C) 2016 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 */
16package com.android.test.uibench.recyclerview;
17
18import android.content.Context;
19import android.graphics.Color;
20import android.support.v7.widget.RecyclerView;
21import android.util.TypedValue;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.List;
28
29public class RvBoxAdapter extends RecyclerView.Adapter<RvBoxAdapter.ViewHolder> {
30
31    private int mBackground;
32
33    private List<String> mValues;
34
35    public static class ViewHolder extends RecyclerView.ViewHolder {
36        public TextView mTextView;
37
38        public ViewHolder(TextView v) {
39            super(v);
40            mTextView = v;
41        }
42
43        @Override
44        public String toString() {
45            return super.toString() + " '" + mTextView.getText();
46        }
47    }
48
49    public RvBoxAdapter(Context context, String[] strings) {
50        TypedValue val = new TypedValue();
51        if (context.getTheme() != null) {
52            context.getTheme().resolveAttribute(
53                    android.R.attr.selectableItemBackground, val, true);
54        }
55        mBackground = val.resourceId;
56        mValues = new ArrayList<>();
57        Collections.addAll(mValues, strings);
58    }
59
60    @Override
61    public RvBoxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
62        final ViewHolder h = new ViewHolder(new TextView(parent.getContext()));
63        h.mTextView.setMinimumHeight(128);
64        h.mTextView.setPadding(20, 0, 20, 0);
65        h.mTextView.setFocusable(true);
66        h.mTextView.setBackgroundResource(mBackground);
67        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
68                ViewGroup.LayoutParams.WRAP_CONTENT,
69                ViewGroup.LayoutParams.WRAP_CONTENT);
70        lp.leftMargin = 10;
71        lp.rightMargin = 5;
72        lp.topMargin = 20;
73        lp.bottomMargin = 15;
74        h.mTextView.setLayoutParams(lp);
75        return h;
76    }
77
78    @Override
79    public void onBindViewHolder(ViewHolder holder, int position) {
80        holder.mTextView.setText(position + ":" + mValues.get(position));
81        holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10));
82        holder.mTextView.setBackgroundColor(getBackgroundColor(position));
83    }
84
85    private int getBackgroundColor(int position) {
86        switch (position % 4) {
87            case 0: return Color.LTGRAY;
88            case 1: return Color.RED;
89            case 2: return Color.DKGRAY;
90            case 3: return Color.BLUE;
91        }
92        return Color.TRANSPARENT;
93    }
94
95    @Override
96    public int getItemCount() {
97        return mValues.size();
98    }
99}
100