Trevor Johns | 6876a9c | 2014-10-17 03:31:00 -0700 | [diff] [blame] | 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.recyclerview; |
| 18 | |
| 19 | import com.example.android.common.logger.Log; |
| 20 | |
| 21 | import android.support.v7.widget.RecyclerView; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | /** |
| 28 | * Provide views to RecyclerView with data from mDataSet. |
| 29 | */ |
| 30 | public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { |
| 31 | private static final String TAG = "CustomAdapter"; |
| 32 | |
| 33 | private String[] mDataSet; |
| 34 | |
| 35 | // BEGIN_INCLUDE(recyclerViewSampleViewHolder) |
| 36 | /** |
| 37 | * Provide a reference to the type of views that you are using (custom viewholder) |
| 38 | */ |
| 39 | public static class ViewHolder extends RecyclerView.ViewHolder { |
| 40 | private final TextView mTextView; |
| 41 | |
| 42 | public ViewHolder(View v) { |
| 43 | super(v); |
| 44 | mTextView = (TextView) v.findViewById(R.id.textView); |
| 45 | } |
| 46 | |
| 47 | public TextView getmTextView() { |
| 48 | return mTextView; |
| 49 | } |
| 50 | } |
| 51 | // END_INCLUDE(recyclerViewSampleViewHolder) |
| 52 | /** |
| 53 | * Initialize the dataset of the Adapter. |
| 54 | * |
| 55 | * @param dataSet String[] containing the data to populate views to be used by RecyclerView. |
| 56 | */ |
| 57 | public CustomAdapter(String[] dataSet) { |
| 58 | mDataSet = dataSet; |
| 59 | } |
| 60 | |
| 61 | // BEGIN_INCLUDE(recyclerViewOnCreateViewHolder) |
| 62 | // Create new views (invoked by the layout manager) |
| 63 | @Override |
| 64 | public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) { |
| 65 | // Create a new view. |
| 66 | View v = LayoutInflater.from(viewGroup.getContext()) |
| 67 | .inflate(R.layout.text_row_item, viewGroup, false); |
| 68 | |
| 69 | ViewHolder vh = new ViewHolder(v); |
| 70 | return vh; |
| 71 | } |
| 72 | // END_INCLUDE(recyclerViewOnCreateViewHolder) |
| 73 | |
| 74 | // BEGIN_INCLUDE(recyclerViewOnBindViewHolder) |
| 75 | // Replace the contents of a view (invoked by the layout manager) |
| 76 | @Override |
| 77 | public void onBindViewHolder(ViewHolder viewHolder, int position) { |
| 78 | Log.d(TAG, "Element " + position + " set."); |
| 79 | |
| 80 | // Get element from your dataset at this position and replace the contents of the view |
| 81 | // with that element |
| 82 | viewHolder.getmTextView().setText(mDataSet[position]); |
| 83 | } |
| 84 | // END_INCLUDE(recyclerViewOnBindViewHolder) |
| 85 | |
| 86 | // Return the size of your dataset (invoked by the layout manager) |
| 87 | @Override |
| 88 | public int getItemCount() { |
| 89 | return mDataSet.length; |
| 90 | } |
| 91 | } |