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