Home | History | Annotate | Download | only in adapter
      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 import java.util.List;
     29 
     30 /**
     31  * A simple RecyclerView adapter that displays every string passed in a constructor as an item.
     32  */
     33 public class SimpleStringAdapter extends RecyclerView.Adapter<SimpleStringAdapter.ViewHolder> {
     34 
     35     private int mBackground;
     36 
     37     private List<String> mValues;
     38 
     39     public static class ViewHolder extends RecyclerView.ViewHolder {
     40         public String mBoundString;
     41         public TextView mTextView;
     42 
     43         public ViewHolder(TextView v) {
     44             super(v);
     45             mTextView = v;
     46         }
     47 
     48         @Override
     49         public String toString() {
     50             return super.toString() + " '" + mTextView.getText();
     51         }
     52     }
     53 
     54     public String getValueAt(int position) {
     55         return mValues.get(position);
     56     }
     57 
     58     public SimpleStringAdapter(Context context, String[] strings) {
     59         TypedValue val = new TypedValue();
     60         if (context.getTheme() != null) {
     61             context.getTheme().resolveAttribute(
     62                     android.R.attr.selectableItemBackground, val, true);
     63         }
     64         mBackground = val.resourceId;
     65         mValues = new ArrayList<String>();
     66         Collections.addAll(mValues, strings);
     67     }
     68 
     69     public void swap(int pos1, int pos2) {
     70         String tmp = mValues.get(pos1);
     71         mValues.set(pos1, mValues.get(pos2));
     72         mValues.set(pos2, tmp);
     73         notifyItemRemoved(pos1);
     74         notifyItemInserted(pos2);
     75     }
     76 
     77     @Override
     78     public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     79         final ViewHolder h = new ViewHolder(new TextView(parent.getContext()));
     80         h.mTextView.setMinimumHeight(128);
     81         h.mTextView.setPadding(20, 0, 20, 0);
     82         h.mTextView.setFocusable(true);
     83         h.mTextView.setBackgroundResource(mBackground);
     84         RecyclerView.LayoutParams lp = getLayoutParams();
     85         h.mTextView.setLayoutParams(lp);
     86         return h;
     87     }
     88 
     89     @Override
     90     public void onBindViewHolder(ViewHolder holder, int position) {
     91         holder.mBoundString = mValues.get(position);
     92         holder.mTextView.setText(position + ":" + mValues.get(position));
     93         holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10));
     94         holder.mTextView.setBackgroundColor(getBackgroundColor(position));
     95     }
     96 
     97 
     98     /**
     99      * Returns LayoutParams to be used for each item in this adapter. It can be overridden
    100      * to provide different LayoutParams.
    101      * @return LayoutParams to be used for each item in this adapter.
    102      */
    103     public RecyclerView.LayoutParams getLayoutParams() {
    104         RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
    105                 ViewGroup.LayoutParams.WRAP_CONTENT,
    106                 ViewGroup.LayoutParams.WRAP_CONTENT);
    107         lp.leftMargin = 10;
    108         lp.rightMargin = 5;
    109         lp.topMargin = 20;
    110         lp.bottomMargin = 15;
    111         return lp;
    112     }
    113 
    114     private int getBackgroundColor(int position) {
    115         switch (position % 4) {
    116             case 0: return Color.BLACK;
    117             case 1: return Color.RED;
    118             case 2: return Color.DKGRAY;
    119             case 3: return Color.BLUE;
    120         }
    121         return Color.TRANSPARENT;
    122     }
    123 
    124     @Override
    125     public int getItemCount() {
    126         return mValues.size();
    127     }
    128 
    129     public List<String> getValues() {
    130         return mValues;
    131     }
    132 
    133     public void setValues(List<String> values) {
    134         mValues = values;
    135     }
    136 }
    137