1 /*
2  * Copyright (C) 2016 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 package com.example.android.wearable.wear.wearnotifications;
17 
18 import android.widget.ImageView;
19 
20 import android.support.v4.app.NotificationCompat;
21 import android.support.wearable.view.WearableRecyclerView;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.TextView;
27 
28 /**
29  * Provides a binding from {@link NotificationCompat.Style} data set to views displayed within the
30  * {@link WearableRecyclerView}.
31  */
32 public class CustomRecyclerAdapter extends
33         WearableRecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {
34 
35     private static final String TAG = "CustomRecyclerAdapter";
36 
37     private String[] mDataSet;
38 
39     // Custom Controller used to instruct main activity to update {@link Notification} and/or
40     // UI for item selected.
41     private Controller mController;
42 
43     /**
44      * Provides reference to the views for each data item. We don't maintain a reference to the
45      * {@link ImageView} (representing the icon), because it does not change for each item. We
46      * wanted to keep the sample simple, but you could add extra code to customize each icon.
47      */
48     public static class ViewHolder extends WearableRecyclerView.ViewHolder {
49 
50         private final TextView mTextView;
51 
ViewHolder(View view)52         public ViewHolder(View view) {
53             super(view);
54             mTextView = (TextView) view.findViewById(R.id.textView);
55         }
56 
57         @Override
toString()58         public String toString() { return (String) mTextView.getText(); }
59     }
60 
CustomRecyclerAdapter(String[] dataSet, Controller controller)61     public CustomRecyclerAdapter(String[] dataSet, Controller controller) {
62         mDataSet = dataSet;
63         mController = controller;
64     }
65 
66     @Override
onCreateViewHolder(ViewGroup viewGroup, int viewType)67     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
68         View view = LayoutInflater.from(viewGroup.getContext())
69                 .inflate(R.layout.recycler_row_item, viewGroup, false);
70 
71         return new ViewHolder(view);
72     }
73 
74     @Override
onBindViewHolder(ViewHolder viewHolder, final int position)75     public void onBindViewHolder(ViewHolder viewHolder, final int position) {
76         Log.d(TAG, "Element " + position + " set.");
77 
78         viewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
79 
80             @Override
81             public void onClick(View view) {
82                 mController.itemSelected(mDataSet[position]);
83             }
84         });
85 
86         // Replaces content of view with correct element from data set
87         viewHolder.mTextView.setText(mDataSet[position]);
88     }
89 
90     // Return the size of your dataset (invoked by the layout manager)
91     @Override
getItemCount()92     public int getItemCount() {
93         return mDataSet.length;
94     }
95 }