1page.title=Creating Lists
2
3@jd:body
4
5<div id="tb-wrapper">
6<div id="tb">
7<h2>This lesson teaches you to</h2>
8<ol>
9  <li><a href="#add-list">Add a List View</a></li>
10  <li><a href="#layout-impl">Create a Layout Implementation for List Items</a></li>
11  <li><a href="#layout-def">Create a Layout Definition for Items</a></li>
12  <li><a href="#adapter">Create an Adapter to Populate the List</a></li>
13  <li><a href="#adapter-listener">Associate the Adapter and Set a Click Listener</a></li>
14</ol>
15<h2>Related Samples</h2>
16<ul>
17  <li><a href="{@docRoot}samples/Notifications/index.html">
18    Notifications</a></li>
19</ul>
20<h2>You should also read</h2>
21<ul>
22  <li><a href="{@docRoot}design/wear/index.html">Android Wear Design Principles</a></li>
23</ul>
24</div>
25</div>
26
27
28<p>Lists let users select an item from a set of choices easily on wearable devices. This lesson
29shows you how to create lists in your Android Wear apps.</p>
30
31<p>The Wearable UI Library includes the
32<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView</code></a>
33class, which is a list implementation optimized for wearable devices.</p>
34
35<p>To create a list in your Android Wear apps:</p>
36
37<ol>
38<li>Add a
39<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView</code></a>
40element to your activity's layout definition.</li>
41<li>Create a custom layout implementation for your list items.</li>
42<li>Use this implementation to create a layout definition file for your list items.</li>
43<li>Create an adapter to populate the list.</li>
44<li>Assign the adapter to the
45<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView</code></a>
46element.</li>
47</ol>
48
49<img src="{@docRoot}wear/images/06_uilib.png" width="200" height="200" alt=""/>
50<p class="img-caption"><strong>Figure 1.</strong>
51A list view on Android Wear.</p>
52
53<p>These steps are described in detail in the following sections.</p>
54
55
56<h2 id="add-list">Add a List View</h2>
57
58<p>The following layout adds a list view to an activity using a
59<a href="{@docRoot}reference/android/support/wearable/view/BoxInsetLayout.html"><code>&lt;BoxInsetLayout&gt;</code></a>
60element, so the list is displayed properly on both round and square devices:</p>
61
62<pre>
63&lt;android.support.wearable.view.BoxInsetLayout
64    xmlns:android="http://schemas.android.com/apk/res/android"
65    xmlns:app="http://schemas.android.com/apk/res-auto"
66    android:background="@drawable/robot_background"
67    android:layout_height="match_parent"
68    android:layout_width="match_parent">
69
70    &lt;FrameLayout
71        android:id="@+id/frame_layout"
72        android:layout_height="match_parent"
73        android:layout_width="match_parent"
74        app:layout_box="left|bottom|right">
75
76        &lt;<strong>android.support.wearable.view.WearableListView</strong>
77            android:id="@+id/wearable_list"
78            android:layout_height="match_parent"
79            android:layout_width="match_parent">
80        &lt;/android.support.wearable.view.WearableListView>
81    &lt;/FrameLayout>
82&lt;/android.support.wearable.view.BoxInsetLayout>
83</pre>
84
85
86<h2 id="layout-impl">Create a Layout Implementation for List Items</h2>
87
88<p>In many cases, each list item consists of an icon and a description. The
89<a href="{@docRoot}samples/Notifications/index.html">Notifications</a> sample implements a custom
90layout that extends
91{@link android.widget.LinearLayout} to incorporate these two elements inside each list item.
92This layout also implements the methods in the
93<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.OnCenterProximityListener.html"<code>WearableListView.OnCenterProximityListener</code></a>
94interface to change the color of the item's icon and fade the text in response to events from the
95<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView</code></a>
96element as the user scrolls through the list.</p>
97
98<pre>
99public class WearableListItemLayout extends LinearLayout
100             implements WearableListView.OnCenterProximityListener {
101
102    private ImageView mCircle;
103    private TextView mName;
104
105    private final float mFadedTextAlpha;
106    private final int mFadedCircleColor;
107    private final int mChosenCircleColor;
108
109    public WearableListItemLayout(Context context) {
110        this(context, null);
111    }
112
113    public WearableListItemLayout(Context context, AttributeSet attrs) {
114        this(context, attrs, 0);
115    }
116
117    public WearableListItemLayout(Context context, AttributeSet attrs,
118                                  int defStyle) {
119        super(context, attrs, defStyle);
120
121        mFadedTextAlpha = getResources()
122                         .getInteger(R.integer.action_text_faded_alpha) / 100f;
123        mFadedCircleColor = getResources().getColor(R.color.grey);
124        mChosenCircleColor = getResources().getColor(R.color.blue);
125    }
126
127    // Get references to the icon and text in the item layout definition
128    &#64;Override
129    protected void onFinishInflate() {
130        super.onFinishInflate();
131        // These are defined in the layout file for list items
132        // (see next section)
133        mCircle = (ImageView) findViewById(R.id.circle);
134        mName = (TextView) findViewById(R.id.name);
135    }
136
137    &#64;Override
138    public void onCenterPosition(boolean animate) {
139        mName.setAlpha(1f);
140        ((GradientDrawable) mCircle.getDrawable()).setColor(mChosenCircleColor);
141    }
142
143    &#64;Override
144    public void onNonCenterPosition(boolean animate) {
145        ((GradientDrawable) mCircle.getDrawable()).setColor(mFadedCircleColor);
146        mName.setAlpha(mFadedTextAlpha);
147    }
148}
149</pre>
150
151<p>You can also create animator objects to enlarge the icon of the center item in the list. You can
152use the
153<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.OnCenterProximityListener.html#onCenterPosition(boolean)"><code>onCenterPosition()</code></a>
154and
155<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.OnCenterProximityListener.html#onNonCenterPosition(boolean)"><code>onNonCenterPosition()</code></a>
156callback methods in the
157<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView.OnCenterProximityListener</code></a>
158interface to manage your animators. For more information about animators, see
159<a href="{@docRoot}guide/topics/graphics/prop-animation.html#object-animator">Animating with
160ObjectAnimator</a>.</p>
161
162
163<h2 id="layout-def">Create a Layout Definition for Items</h2>
164
165<p>After you implement a custom layout for list items, you provide a layout definition file that
166specifies the layout parameters of each of the components inside a list item. The following layout
167definition uses the custom layout implementation from the previous section and defines an icon
168and a text view whose IDs match those in the layout implementation class:</p>
169
170<p class="code-caption">res/layout/list_item.xml</p>
171
172<pre>
173&lt;com.example.android.support.wearable.notifications.WearableListItemLayout
174    xmlns:android="http://schemas.android.com/apk/res/android"
175    android:gravity="center_vertical"
176    android:layout_width="match_parent"
177    android:layout_height="80dp">
178    &lt;ImageView
179        android:id="@+id/circle"
180        android:layout_height="20dp"
181        android:layout_margin="16dp"
182        android:layout_width="20dp"
183        android:src="@drawable/wl_circle"/>
184    &lt;TextView
185        android:id="@+id/name"
186        android:gravity="center_vertical|left"
187        android:layout_width="wrap_content"
188        android:layout_marginRight="16dp"
189        android:layout_height="match_parent"
190        android:fontFamily="sans-serif-condensed-light"
191        android:lineSpacingExtra="-4sp"
192        android:textColor="@color/text_color"
193        android:textSize="16sp"/>
194&lt;/com.example.android.support.wearable.notifications.WearableListItemLayout>
195</pre>
196
197
198<h2 id="adapter">Create an Adapter to Populate the List</h2>
199
200<p>The adapter populates the
201<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView.OnCenterProximityListener</code></a>
202element with content. The following simple
203adapter populates the list with elements based on an array of strings:</p>
204
205<pre>
206private static final class Adapter extends WearableListView.Adapter {
207    private String[] mDataset;
208    private final Context mContext;
209    private final LayoutInflater mInflater;
210
211    // Provide a suitable constructor (depends on the kind of dataset)
212    public Adapter(Context context, String[] dataset) {
213        mContext = context;
214        mInflater = LayoutInflater.from(context);
215        mDataset = dataset;
216    }
217
218    // Provide a reference to the type of views you're using
219    public static class ItemViewHolder extends WearableListView.ViewHolder {
220        private TextView textView;
221        public ItemViewHolder(View itemView) {
222            super(itemView);
223            // find the text view within the custom item's layout
224            textView = (TextView) itemView.findViewById(R.id.name);
225        }
226    }
227
228    // Create new views for list items
229    // (invoked by the WearableListView's layout manager)
230    &#64;Override
231    public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent,
232                                                          int viewType) {
233        // Inflate our custom layout for list items
234        return new ItemViewHolder(mInflater.inflate(R.layout.list_item, null));
235    }
236
237    // Replace the contents of a list item
238    // Instead of creating new views, the list tries to recycle existing ones
239    // (invoked by the WearableListView's layout manager)
240    &#64;Override
241    public void onBindViewHolder(WearableListView.ViewHolder holder,
242                                 int position) {
243        // retrieve the text view
244        ItemViewHolder itemHolder = (ItemViewHolder) holder;
245        TextView view = itemHolder.textView;
246        // replace text contents
247        view.setText(mDataset[position]);
248        // replace list item's metadata
249        holder.itemView.setTag(position);
250    }
251
252    // Return the size of your dataset
253    // (invoked by the WearableListView's layout manager)
254    &#64;Override
255    public int getItemCount() {
256        return mDataset.length;
257    }
258}
259</pre>
260
261
262<h2 id="adapter-listener">Associate the Adapter and Set a Click Listener</h2>
263
264<p>In your activity, obtain a reference to the
265<a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView.OnCenterProximityListener</code></a>
266element from your layout, assign an instance of the adapter to populate the list, and set a click
267listener to complete an action when the user selects a particular list item.</p>
268
269<pre>
270public class WearActivity extends Activity
271                          implements WearableListView.ClickListener {
272
273    // Sample dataset for the list
274    String[] elements = { "List Item 1", "List Item 2", ... };
275
276    &#64;Override
277    protected void onCreate(Bundle savedInstanceState) {
278        super.onCreate(savedInstanceState);
279        setContentView(R.layout.my_list_activity);
280
281        // Get the list component from the layout of the activity
282        WearableListView listView =
283            (WearableListView) findViewById(R.id.wearable_list);
284
285        // Assign an adapter to the list
286        listView.setAdapter(new Adapter(this, elements));
287
288        // Set a click listener
289        listView.setClickListener(this);
290    }
291
292    // WearableListView click listener
293    &#64;Override
294    public void onClick(WearableListView.ViewHolder v) {
295        Integer tag = (Integer) v.itemView.getTag();
296        // use this data to complete some action ...
297    }
298
299    &#64;Override
300    public void onTopEmptyRegionClick() {
301    }
302}
303</pre>
304