ListFragment.java revision 445646c52128a763b56ed7bb3bd009e2f33e3e4f
15ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn/*
25ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Copyright (C) 2010 The Android Open Source Project
35ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
45ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
55ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * you may not use this file except in compliance with the License.
65ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * You may obtain a copy of the License at
75ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
85ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
95ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
105ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
115ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
125ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * See the License for the specific language governing permissions and
145ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * limitations under the License.
155ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn */
165ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
175ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornpackage android.app;
185ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
195ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.os.Bundle;
205ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.os.Handler;
215ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.view.LayoutInflater;
225ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.view.View;
235ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.view.ViewGroup;
24445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackbornimport android.view.animation.AnimationUtils;
255ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.widget.AdapterView;
265ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.widget.ListAdapter;
275ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornimport android.widget.ListView;
28445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackbornimport android.widget.TextView;
295ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
305ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn/**
315ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * An fragment that displays a list of items by binding to a data source such as
325ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * an array or Cursor, and exposes event handlers when the user selects an item.
335ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
345ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * ListActivity hosts a {@link android.widget.ListView ListView} object that can
355ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * be bound to different data sources, typically either an array or a Cursor
365ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * holding query results. Binding, screen layout, and row layout are discussed
375ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * in the following sections.
385ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
395ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <strong>Screen Layout</strong>
405ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
415ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
425ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * ListActivity has a default layout that consists of a single list view.
435ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * However, if you desire, you can customize the fragment layout by returning
445ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * your own view hierarchy from {@link #onCreateView}.
455ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * To do this, your view hierarchy MUST contain a ListView object with the
465ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * id "@android:id/list" (or {@link android.R.id#list} if it's in code)
475ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
485ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Optionally, your view hierarchy can contain another view object of any type to
495ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * display when the list view is empty. This "empty list" notifier must have an
505ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * id "android:empty". Note that when an empty view is present, the list view
515ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * will be hidden when there is no data to display.
525ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
535ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * The following code demonstrates an (ugly) custom lisy layout. It has a list
545ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * with a green background, and an alternate red "no data" message.
555ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
565ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
575ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <pre>
585ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
595ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
605ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:orientation=&quot;vertical&quot;
615ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_width=&quot;match_parent&quot;
625ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_height=&quot;match_parent&quot;
635ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:paddingLeft=&quot;8dp&quot;
645ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:paddingRight=&quot;8dp&quot;&gt;
655ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
665ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     &lt;ListView android:id=&quot;@id/android:list&quot;
675ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:layout_width=&quot;match_parent&quot;
685ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:layout_height=&quot;match_parent&quot;
695ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:background=&quot;#00FF00&quot;
705ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:layout_weight=&quot;1&quot;
715ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:drawSelectorOnTop=&quot;false&quot;/&gt;
725ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
735ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     &lt;TextView android:id=&quot;@id/android:empty&quot;
745ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:layout_width=&quot;match_parent&quot;
755ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:layout_height=&quot;match_parent&quot;
765ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:background=&quot;#FF0000&quot;
775ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *               android:text=&quot;No data&quot;/&gt;
785ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;/LinearLayout&gt;
795ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </pre>
805ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
815ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
825ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <strong>Row Layout</strong>
835ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
845ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
855ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * You can specify the layout of individual rows in the list. You do this by
865ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * specifying a layout resource in the ListAdapter object hosted by the fragment
875ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * (the ListAdapter binds the ListView to the data; more on this later).
885ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
895ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * A ListAdapter constructor takes a parameter that specifies a layout resource
905ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * for each row. It also has two additional parameters that let you specify
915ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * which data field to associate with which object in the row layout resource.
925ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * These two parameters are typically parallel arrays.
935ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
945ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
955ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Android provides some standard row layout resources. These are in the
965ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * {@link android.R.layout} class, and have names such as simple_list_item_1,
975ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * simple_list_item_2, and two_line_list_item. The following layout XML is the
985ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * source for the resource two_line_list_item, which displays two data
995ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * fields,one above the other, for each list row.
1005ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
1015ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
1025ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <pre>
1035ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
1045ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1055ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     android:layout_width=&quot;match_parent&quot;
1065ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     android:layout_height=&quot;wrap_content&quot;
1075ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     android:orientation=&quot;vertical&quot;&gt;
1085ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
1095ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     &lt;TextView android:id=&quot;@+id/text1&quot;
1105ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:textSize=&quot;16sp&quot;
1115ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:textStyle=&quot;bold&quot;
1125ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_width=&quot;match_parent&quot;
1135ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_height=&quot;wrap_content&quot;/&gt;
1145ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
1155ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *     &lt;TextView android:id=&quot;@+id/text2&quot;
1165ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:textSize=&quot;16sp&quot;
1175ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_width=&quot;match_parent&quot;
1185ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *         android:layout_height=&quot;wrap_content&quot;/&gt;
1195ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * &lt;/LinearLayout&gt;
1205ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </pre>
1215ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
1225ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
1235ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * You must identify the data bound to each TextView object in this layout. The
1245ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * syntax for this is discussed in the next section.
1255ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
1265ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
1275ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <strong>Binding to Data</strong>
1285ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
1295ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * <p>
1305ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * You bind the ListFragment's ListView object to data using a class that
1315ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * implements the {@link android.widget.ListAdapter ListAdapter} interface.
1325ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * Android provides two standard list adapters:
1335ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * {@link android.widget.SimpleAdapter SimpleAdapter} for static data (Maps),
1345ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * and {@link android.widget.SimpleCursorAdapter SimpleCursorAdapter} for Cursor
1355ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * query results.
1365ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * </p>
1375ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn *
1385ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * @see #setListAdapter
1395ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn * @see android.widget.ListView
1405ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn */
1415ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackbornpublic class ListFragment extends Fragment {
1425ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    final private Handler mHandler = new Handler();
1435ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1445ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    final private Runnable mRequestFocus = new Runnable() {
1455ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        public void run() {
1465ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            mList.focusableViewAvailable(mList);
1475ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
1485ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    };
1495ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1505ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    final private AdapterView.OnItemClickListener mOnClickListener
1515ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            = new AdapterView.OnItemClickListener() {
1525ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
1535ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            onListItemClick((ListView)parent, v, position, id);
1545ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
1555ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    };
1565ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1575ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    ListAdapter mAdapter;
1585ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    ListView mList;
159445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    View mEmptyView;
160445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    TextView mStandardEmptyView;
161445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    View mProgressContainer;
162445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    View mListContainer;
163445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    boolean mSetEmptyView;
164445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    boolean mListShown;
1655ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1665ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public ListFragment() {
1675ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
1685ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1695ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
170445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * Provide default implementation to return a simple list view.  Subclasses
171445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * can override to replace with their own layout.  If doing so, the
172445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * returned view hierarchy <em>must</em> have a ListView whose id
173445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * is {@link android.R.id.list android.R.id.list} and can optionally
174445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * have a sibling view id {@link android.R.id.empty android.R.id.empty}
175445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * that is to be shown when the list is empty.
1765ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
1775ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    @Override
1785ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public View onCreateView(LayoutInflater inflater, ViewGroup container,
1795ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            Bundle savedInstanceState) {
180445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        return inflater.inflate(com.android.internal.R.layout.list_content_rich,
1815ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn                container, false);
1825ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
1835ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1845ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
1855ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Attach to list view once Fragment is ready to run.
1865ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
1875ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    @Override
1885ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public void onReady(Bundle savedInstanceState) {
1895ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        super.onReady(savedInstanceState);
1905ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        ensureList();
1915ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
1925ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
1935ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
1945ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Detach from list view.
1955ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
1965ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    @Override
1975ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public void onDestroyView() {
1985ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mHandler.removeCallbacks(mRequestFocus);
1995ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mList = null;
2005ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        super.onDestroyView();
2015ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2025ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2035ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2045ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * This method will be called when an item in the list is selected.
2055ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Subclasses should override. Subclasses can call
2065ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * getListView().getItemAtPosition(position) if they need to access the
2075ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * data associated with the selected item.
2085ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     *
2095ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * @param l The ListView where the click happened
2105ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * @param v The view that was clicked within the ListView
2115ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * @param position The position of the view in the list
2125ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * @param id The row id of the item that was clicked
2135ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2145ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public void onListItemClick(ListView l, View v, int position, long id) {
2155ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2165ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2175ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2185ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Provide the cursor for the list view.
2195ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2205ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public void setListAdapter(ListAdapter adapter) {
2215ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mAdapter = adapter;
2225ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        if (mList != null) {
2235ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            mList.setAdapter(adapter);
2245ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
2255ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2265ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2275ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2285ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Set the currently selected list item to the specified
2295ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * position with the adapter's data
2305ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     *
2315ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * @param position
2325ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2335ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public void setSelection(int position) {
2345ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        ensureList();
2355ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mList.setSelection(position);
2365ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2375ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2385ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2395ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Get the position of the currently selected list item.
2405ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2415ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public int getSelectedItemPosition() {
2425ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        ensureList();
2435ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        return mList.getSelectedItemPosition();
2445ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2455ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2465ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2475ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Get the cursor row ID of the currently selected list item.
2485ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2495ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public long getSelectedItemId() {
2505ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        ensureList();
2515ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        return mList.getSelectedItemId();
2525ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2535ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2545ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
2555ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Get the activity's list view widget.
2565ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
2575ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public ListView getListView() {
2585ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        ensureList();
2595ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        return mList;
2605ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
2615ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2625ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    /**
263445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * The default content for a ListFragment has a TextView that can
264445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * be shown when the list is empty.  If you would like to have it
265445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * shown, call this method to supply the text it should use.
266445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     */
267445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    public void setEmptyText(CharSequence text) {
268445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        ensureList();
269445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (mStandardEmptyView == null) {
270445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            throw new IllegalStateException("Can't be used with a custom content view");
271445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
272445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (!mSetEmptyView) {
273445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mSetEmptyView = true;
274445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mList.setEmptyView(mStandardEmptyView);
275445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
276445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    }
277445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
278445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    /**
279445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * Control whether the list is being displayed.  You can make it not
280445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * displayed if you are waiting for the initial data to show in it.  During
281445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * this time an indeterminant progress indicator will be shown instead.
282445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     *
283445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * @param shown If true, the list view is shown; if false, the progress
284445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * indicator.  The initial value is true.
285445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * @param animate If true, an animation will be used to transition to the
286445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * new state.
287445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     */
288445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    public void setListShown(boolean shown, boolean animate) {
289445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        ensureList();
290445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (mProgressContainer == null) {
291445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            throw new IllegalStateException("Can't be used with a custom content view");
292445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
293445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (mListShown == shown) {
294445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            return;
295445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
296445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        mListShown = shown;
297445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (shown) {
298445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (animate) {
299445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
300445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        getActivity(), android.R.anim.fade_out));
301445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mListContainer.startAnimation(AnimationUtils.loadAnimation(
302445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        getActivity(), android.R.anim.fade_in));
303445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
304445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mProgressContainer.setVisibility(View.GONE);
305445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mListContainer.setVisibility(View.VISIBLE);
306445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        } else {
307445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (animate) {
308445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
309445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        getActivity(), android.R.anim.fade_in));
310445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mListContainer.startAnimation(AnimationUtils.loadAnimation(
311445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        getActivity(), android.R.anim.fade_out));
312445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
313445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mProgressContainer.setVisibility(View.VISIBLE);
314445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mListContainer.setVisibility(View.GONE);
315445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
316445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    }
317445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
318445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    /**
3195ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     * Get the ListAdapter associated with this activity's ListView.
3205ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn     */
3215ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public ListAdapter getListAdapter() {
3225ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        return mAdapter;
3235ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
3245ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
3255ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    private void ensureList() {
3265ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        if (mList != null) {
3275ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            return;
3285ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
3295ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        View root = getView();
3305ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        if (root == null) {
3315ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            throw new IllegalStateException("Content view not yet created");
3325ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
333445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (root instanceof ListView) {
334445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mList = (ListView)root;
335445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        } else {
336445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mStandardEmptyView = (TextView)root.findViewById(
337445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                    com.android.internal.R.id.internalEmpty);
338445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (mStandardEmptyView == null) {
339445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mEmptyView = root.findViewById(android.R.id.empty);
340445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
341445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mProgressContainer = root.findViewById(com.android.internal.R.id.progressContainer);
342445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mListContainer = root.findViewById(com.android.internal.R.id.listContainer);
343445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            View rawListView = root.findViewById(android.R.id.list);
344445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (!(rawListView instanceof ListView)) {
345445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                throw new RuntimeException(
346445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        "Content has view with id attribute 'android.R.id.list' "
347445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        + "that is not a ListView class");
348445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
349445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mList = (ListView)rawListView;
350445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (mList == null) {
351445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                throw new RuntimeException(
352445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        "Your content must have a ListView whose id attribute is " +
353445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                        "'android.R.id.list'");
354445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
355445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (mEmptyView != null) {
356445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mList.setEmptyView(mEmptyView);
357445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
3585ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
359445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        mListShown = true;
3605ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mList.setOnItemClickListener(mOnClickListener);
3615ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        if (mAdapter != null) {
3625ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            setListAdapter(mAdapter);
3635ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
3645ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        mHandler.post(mRequestFocus);
3655ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
3665ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn}
367