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 17 package com.example.android.supportv7.widget.adapter; 18 19 import android.content.Context; 20 import android.graphics.Color; 21 import android.util.TypedValue; 22 import android.view.ViewGroup; 23 import android.widget.TextView; 24 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import java.util.ArrayList; 28 import java.util.Collections; 29 import java.util.List; 30 31 /** 32 * A simple RecyclerView adapter that displays every string passed in a constructor as an item. 33 */ 34 public 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 ViewHolder(TextView v)44 public ViewHolder(TextView v) { 45 super(v); 46 mTextView = v; 47 } 48 49 @Override toString()50 public String toString() { 51 return super.toString() + " '" + mTextView.getText(); 52 } 53 } 54 getValueAt(int position)55 public String getValueAt(int position) { 56 return mValues.get(position); 57 } 58 SimpleStringAdapter(Context context, String[] strings)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 swap(int pos1, int pos2)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 onCreateViewHolder(ViewGroup parent, int viewType)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 onBindViewHolder(ViewHolder holder, int position)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 */ getLayoutParams()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 getBackgroundColor(int position)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 getItemCount()126 public int getItemCount() { 127 return mValues.size(); 128 } 129 getValues()130 public List<String> getValues() { 131 return mValues; 132 } 133 setValues(List<String> values)134 public void setValues(List<String> values) { 135 mValues = values; 136 } 137 } 138