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 textView; 41 ViewHolder(View v)42 public ViewHolder(View v) { 43 super(v); 44 // Define click listener for the ViewHolder's View. 45 v.setOnClickListener(new View.OnClickListener() { 46 @Override 47 public void onClick(View v) { 48 Log.d(TAG, "Element " + getPosition() + " clicked."); 49 } 50 }); 51 textView = (TextView) v.findViewById(R.id.textView); 52 } 53 getTextView()54 public TextView getTextView() { 55 return textView; 56 } 57 } 58 // END_INCLUDE(recyclerViewSampleViewHolder) 59 60 /** 61 * Initialize the dataset of the Adapter. 62 * 63 * @param dataSet String[] containing the data to populate views to be used by RecyclerView. 64 */ CustomAdapter(String[] dataSet)65 public CustomAdapter(String[] dataSet) { 66 mDataSet = dataSet; 67 } 68 69 // BEGIN_INCLUDE(recyclerViewOnCreateViewHolder) 70 // Create new views (invoked by the layout manager) 71 @Override onCreateViewHolder(ViewGroup viewGroup, int viewType)72 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 73 // Create a new view. 74 View v = LayoutInflater.from(viewGroup.getContext()) 75 .inflate(R.layout.text_row_item, viewGroup, false); 76 77 return new ViewHolder(v); 78 } 79 // END_INCLUDE(recyclerViewOnCreateViewHolder) 80 81 // BEGIN_INCLUDE(recyclerViewOnBindViewHolder) 82 // Replace the contents of a view (invoked by the layout manager) 83 @Override onBindViewHolder(ViewHolder viewHolder, final int position)84 public void onBindViewHolder(ViewHolder viewHolder, final int position) { 85 Log.d(TAG, "Element " + position + " set."); 86 87 // Get element from your dataset at this position and replace the contents of the view 88 // with that element 89 viewHolder.getTextView().setText(mDataSet[position]); 90 } 91 // END_INCLUDE(recyclerViewOnBindViewHolder) 92 93 // Return the size of your dataset (invoked by the layout manager) 94 @Override getItemCount()95 public int getItemCount() { 96 return mDataSet.length; 97 } 98 } 99