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