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  */
16 package com.android.test.uibench.recyclerview;
17 
18 import android.content.Context;
19 import android.graphics.Color;
20 import androidx.recyclerview.widget.RecyclerView;
21 import android.util.TypedValue;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24 
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 
29 public 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 
ViewHolder(TextView v)38         public ViewHolder(TextView v) {
39             super(v);
40             mTextView = v;
41         }
42 
43         @Override
toString()44         public String toString() {
45             return super.toString() + " '" + mTextView.getText();
46         }
47     }
48 
RvBoxAdapter(Context context, String[] strings)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
onCreateViewHolder(ViewGroup parent, int viewType)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
onBindViewHolder(ViewHolder holder, int position)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 
getBackgroundColor(int position)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
getItemCount()96     public int getItemCount() {
97         return mValues.size();
98     }
99 }
100