| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
} |