1*90c8c64dSAndroid Build Coastguard Worker /*
2*90c8c64dSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*90c8c64dSAndroid Build Coastguard Worker *
4*90c8c64dSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*90c8c64dSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*90c8c64dSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*90c8c64dSAndroid Build Coastguard Worker *
8*90c8c64dSAndroid Build Coastguard Worker *      http://www.apache.org/licenses/LICENSE-2.0
9*90c8c64dSAndroid Build Coastguard Worker *
10*90c8c64dSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*90c8c64dSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*90c8c64dSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*90c8c64dSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*90c8c64dSAndroid Build Coastguard Worker * limitations under the License.
15*90c8c64dSAndroid Build Coastguard Worker */
16*90c8c64dSAndroid Build Coastguard Worker 
17*90c8c64dSAndroid Build Coastguard Worker package com.example.android.recyclerview;
18*90c8c64dSAndroid Build Coastguard Worker 
19*90c8c64dSAndroid Build Coastguard Worker import com.example.android.common.logger.Log;
20*90c8c64dSAndroid Build Coastguard Worker 
21*90c8c64dSAndroid Build Coastguard Worker import android.support.v7.widget.RecyclerView;
22*90c8c64dSAndroid Build Coastguard Worker import android.view.LayoutInflater;
23*90c8c64dSAndroid Build Coastguard Worker import android.view.View;
24*90c8c64dSAndroid Build Coastguard Worker import android.view.ViewGroup;
25*90c8c64dSAndroid Build Coastguard Worker import android.widget.TextView;
26*90c8c64dSAndroid Build Coastguard Worker 
27*90c8c64dSAndroid Build Coastguard Worker /**
28*90c8c64dSAndroid Build Coastguard Worker  * Provide views to RecyclerView with data from mDataSet.
29*90c8c64dSAndroid Build Coastguard Worker  */
30*90c8c64dSAndroid Build Coastguard Worker public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
31*90c8c64dSAndroid Build Coastguard Worker     private static final String TAG = "CustomAdapter";
32*90c8c64dSAndroid Build Coastguard Worker 
33*90c8c64dSAndroid Build Coastguard Worker     private String[] mDataSet;
34*90c8c64dSAndroid Build Coastguard Worker 
35*90c8c64dSAndroid Build Coastguard Worker     // BEGIN_INCLUDE(recyclerViewSampleViewHolder)
36*90c8c64dSAndroid Build Coastguard Worker     /**
37*90c8c64dSAndroid Build Coastguard Worker      * Provide a reference to the type of views that you are using (custom ViewHolder)
38*90c8c64dSAndroid Build Coastguard Worker      */
39*90c8c64dSAndroid Build Coastguard Worker     public static class ViewHolder extends RecyclerView.ViewHolder {
40*90c8c64dSAndroid Build Coastguard Worker         private final TextView textView;
41*90c8c64dSAndroid Build Coastguard Worker 
ViewHolder(View v)42*90c8c64dSAndroid Build Coastguard Worker         public ViewHolder(View v) {
43*90c8c64dSAndroid Build Coastguard Worker             super(v);
44*90c8c64dSAndroid Build Coastguard Worker             // Define click listener for the ViewHolder's View.
45*90c8c64dSAndroid Build Coastguard Worker             v.setOnClickListener(new View.OnClickListener() {
46*90c8c64dSAndroid Build Coastguard Worker                 @Override
47*90c8c64dSAndroid Build Coastguard Worker                 public void onClick(View v) {
48*90c8c64dSAndroid Build Coastguard Worker                     Log.d(TAG, "Element " + getAdapterPosition() + " clicked.");
49*90c8c64dSAndroid Build Coastguard Worker                 }
50*90c8c64dSAndroid Build Coastguard Worker             });
51*90c8c64dSAndroid Build Coastguard Worker             textView = (TextView) v.findViewById(R.id.textView);
52*90c8c64dSAndroid Build Coastguard Worker         }
53*90c8c64dSAndroid Build Coastguard Worker 
getTextView()54*90c8c64dSAndroid Build Coastguard Worker         public TextView getTextView() {
55*90c8c64dSAndroid Build Coastguard Worker             return textView;
56*90c8c64dSAndroid Build Coastguard Worker         }
57*90c8c64dSAndroid Build Coastguard Worker     }
58*90c8c64dSAndroid Build Coastguard Worker     // END_INCLUDE(recyclerViewSampleViewHolder)
59*90c8c64dSAndroid Build Coastguard Worker 
60*90c8c64dSAndroid Build Coastguard Worker     /**
61*90c8c64dSAndroid Build Coastguard Worker      * Initialize the dataset of the Adapter.
62*90c8c64dSAndroid Build Coastguard Worker      *
63*90c8c64dSAndroid Build Coastguard Worker      * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
64*90c8c64dSAndroid Build Coastguard Worker      */
CustomAdapter(String[] dataSet)65*90c8c64dSAndroid Build Coastguard Worker     public CustomAdapter(String[] dataSet) {
66*90c8c64dSAndroid Build Coastguard Worker         mDataSet = dataSet;
67*90c8c64dSAndroid Build Coastguard Worker     }
68*90c8c64dSAndroid Build Coastguard Worker 
69*90c8c64dSAndroid Build Coastguard Worker     // BEGIN_INCLUDE(recyclerViewOnCreateViewHolder)
70*90c8c64dSAndroid Build Coastguard Worker     // Create new views (invoked by the layout manager)
71*90c8c64dSAndroid Build Coastguard Worker     @Override
onCreateViewHolder(ViewGroup viewGroup, int viewType)72*90c8c64dSAndroid Build Coastguard Worker     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
73*90c8c64dSAndroid Build Coastguard Worker         // Create a new view.
74*90c8c64dSAndroid Build Coastguard Worker         View v = LayoutInflater.from(viewGroup.getContext())
75*90c8c64dSAndroid Build Coastguard Worker                 .inflate(R.layout.text_row_item, viewGroup, false);
76*90c8c64dSAndroid Build Coastguard Worker 
77*90c8c64dSAndroid Build Coastguard Worker         return new ViewHolder(v);
78*90c8c64dSAndroid Build Coastguard Worker     }
79*90c8c64dSAndroid Build Coastguard Worker     // END_INCLUDE(recyclerViewOnCreateViewHolder)
80*90c8c64dSAndroid Build Coastguard Worker 
81*90c8c64dSAndroid Build Coastguard Worker     // BEGIN_INCLUDE(recyclerViewOnBindViewHolder)
82*90c8c64dSAndroid Build Coastguard Worker     // Replace the contents of a view (invoked by the layout manager)
83*90c8c64dSAndroid Build Coastguard Worker     @Override
onBindViewHolder(ViewHolder viewHolder, final int position)84*90c8c64dSAndroid Build Coastguard Worker     public void onBindViewHolder(ViewHolder viewHolder, final int position) {
85*90c8c64dSAndroid Build Coastguard Worker         Log.d(TAG, "Element " + position + " set.");
86*90c8c64dSAndroid Build Coastguard Worker 
87*90c8c64dSAndroid Build Coastguard Worker         // Get element from your dataset at this position and replace the contents of the view
88*90c8c64dSAndroid Build Coastguard Worker         // with that element
89*90c8c64dSAndroid Build Coastguard Worker         viewHolder.getTextView().setText(mDataSet[position]);
90*90c8c64dSAndroid Build Coastguard Worker     }
91*90c8c64dSAndroid Build Coastguard Worker     // END_INCLUDE(recyclerViewOnBindViewHolder)
92*90c8c64dSAndroid Build Coastguard Worker 
93*90c8c64dSAndroid Build Coastguard Worker     // Return the size of your dataset (invoked by the layout manager)
94*90c8c64dSAndroid Build Coastguard Worker     @Override
getItemCount()95*90c8c64dSAndroid Build Coastguard Worker     public int getItemCount() {
96*90c8c64dSAndroid Build Coastguard Worker         return mDataSet.length;
97*90c8c64dSAndroid Build Coastguard Worker     }
98*90c8c64dSAndroid Build Coastguard Worker }
99