ListFragment.java revision 9c53b844bd525e6a04e17291efc38713893074cd
1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.app;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler;
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Gravity;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.LayoutInflater;
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View;
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.AnimationUtils;
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.AdapterView;
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.FrameLayout;
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ListAdapter;
29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ListView;
30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.TextView;
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.ListFragment}.
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation.  See the framework SDK
37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class ListFragment extends Fragment {
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static final int INTERNAL_EMPTY_ID = 0x00ff0001;
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    final private Handler mHandler = new Handler();
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    final private Runnable mRequestFocus = new Runnable() {
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void run() {
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mList.focusableViewAvailable(mList);
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    };
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    final private AdapterView.OnItemClickListener mOnClickListener
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            = new AdapterView.OnItemClickListener() {
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            onListItemClick((ListView)parent, v, position, id);
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    };
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ListAdapter mAdapter;
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ListView mList;
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    View mEmptyView;
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    TextView mStandardEmptyView;
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    View mProgressContainer;
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    View mListContainer;
639c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn    CharSequence mEmptyText;
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mListShown;
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public ListFragment() {
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Provide default implementation to return a simple list view.  Subclasses
71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * can override to replace with their own layout.  If doing so, the
72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * returned view hierarchy <em>must</em> have a ListView whose id
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is {@link android.R.id#list android.R.id.list} and can optionally
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * have a sibling view id {@link android.R.id#empty android.R.id.empty}
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * that is to be shown when the list is empty.
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>If you are overriding this method with your own custom content,
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * consider including the standard layout {@link android.R.layout#list_content}
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * in your layout file, so that you continue to retain all of the standard
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * behavior of ListFragment.  In particular, this is currently the only
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * way to have the built-in indeterminant progress state be shown.
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public View onCreateView(LayoutInflater inflater, ViewGroup container,
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            Bundle savedInstanceState) {
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FrameLayout root = new FrameLayout(getActivity());
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        TextView tv = new TextView(getActivity());
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tv.setId(INTERNAL_EMPTY_ID);
90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tv.setGravity(Gravity.CENTER);
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        root.addView(tv, new FrameLayout.LayoutParams(
92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ListView lv = new ListView(getActivity());
95cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        lv.setId(android.R.id.list);
96cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        lv.setDrawSelectorOnTop(false);
97cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        root.addView(lv, new FrameLayout.LayoutParams(
98cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ListView.LayoutParams lp = new ListView.LayoutParams(
101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        root.setLayoutParams(lp);
103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return root;
105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
108e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn     * Attach to list view once the view hierarchy has been created.
109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
111e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    public void onViewCreated(View view, Bundle savedInstanceState) {
112e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        super.onViewCreated(view, savedInstanceState);
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Detach from list view.
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onDestroyView() {
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mHandler.removeCallbacks(mRequestFocus);
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mList = null;
1239c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        mListShown = false;
1249c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        mEmptyView = mProgressContainer = mListContainer = null;
1259c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        mStandardEmptyView = null;
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onDestroyView();
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This method will be called when an item in the list is selected.
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses should override. Subclasses can call
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * getListView().getItemAtPosition(position) if they need to access the
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * data associated with the selected item.
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param l The ListView where the click happened
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param v The view that was clicked within the ListView
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param position The position of the view in the list
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param id The row id of the item that was clicked
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onListItemClick(ListView l, View v, int position, long id) {
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Provide the cursor for the list view.
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setListAdapter(ListAdapter adapter) {
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean hadAdapter = mAdapter != null;
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAdapter = adapter;
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mList != null) {
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mList.setAdapter(adapter);
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (!mListShown && !hadAdapter) {
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // The list was hidden, and previously didn't have an
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // adapter.  It is now time to show it.
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                setListShown(true, getView().getWindowToken() != null);
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Set the currently selected list item to the specified
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * position with the adapter's data
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param position
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setSelection(int position) {
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mList.setSelection(position);
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Get the position of the currently selected list item.
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getSelectedItemPosition() {
174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mList.getSelectedItemPosition();
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Get the cursor row ID of the currently selected list item.
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public long getSelectedItemId() {
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mList.getSelectedItemId();
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Get the activity's list view widget.
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public ListView getListView() {
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mList;
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The default content for a ListFragment has a TextView that can
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * be shown when the list is empty.  If you would like to have it
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * shown, call this method to supply the text it should use.
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setEmptyText(CharSequence text) {
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStandardEmptyView == null) {
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("Can't be used with a custom content view");
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStandardEmptyView.setText(text);
2059c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        if (mEmptyText == null) {
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mList.setEmptyView(mStandardEmptyView);
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2089c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        mEmptyText = text;
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Control whether the list is being displayed.  You can make it not
213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * displayed if you are waiting for the initial data to show in it.  During
214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * this time an indeterminant progress indicator will be shown instead.
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Applications do not normally need to use this themselves.  The default
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * behavior of ListFragment is to start with the list not being shown, only
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * showing it once an adapter is given with {@link #setListAdapter(ListAdapter)}.
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If the list at that point had not been shown, when it does get shown
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * it will be do without the user ever seeing the hidden state.
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param shown If true, the list view is shown; if false, the progress
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * indicator.  The initial value is true.
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setListShown(boolean shown) {
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        setListShown(shown, true);
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Like {@link #setListShown(boolean)}, but no animation is used when
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * transitioning from the previous state.
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setListShownNoAnimation(boolean shown) {
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        setListShown(shown, false);
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Control whether the list is being displayed.  You can make it not
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * displayed if you are waiting for the initial data to show in it.  During
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * this time an indeterminant progress indicator will be shown instead.
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param shown If true, the list view is shown; if false, the progress
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * indicator.  The initial value is true.
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param animate If true, an animation will be used to transition to the
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * new state.
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private void setListShown(boolean shown, boolean animate) {
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ensureList();
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mProgressContainer == null) {
250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("Can't be used with a custom content view");
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mListShown == shown) {
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mListShown = shown;
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (shown) {
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (animate) {
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        getActivity(), android.R.anim.fade_out));
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mListContainer.startAnimation(AnimationUtils.loadAnimation(
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        getActivity(), android.R.anim.fade_in));
2629c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            } else {
2639c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mProgressContainer.clearAnimation();
2649c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mListContainer.clearAnimation();
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mProgressContainer.setVisibility(View.GONE);
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mListContainer.setVisibility(View.VISIBLE);
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (animate) {
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        getActivity(), android.R.anim.fade_in));
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mListContainer.startAnimation(AnimationUtils.loadAnimation(
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        getActivity(), android.R.anim.fade_out));
2749c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            } else {
2759c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mProgressContainer.clearAnimation();
2769c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mListContainer.clearAnimation();
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mProgressContainer.setVisibility(View.VISIBLE);
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mListContainer.setVisibility(View.GONE);
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Get the ListAdapter associated with this activity's ListView.
285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public ListAdapter getListAdapter() {
287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mAdapter;
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private void ensureList() {
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mList != null) {
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        View root = getView();
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (root == null) {
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("Content view not yet created");
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (root instanceof ListView) {
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mList = (ListView)root;
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mStandardEmptyView = (TextView)root.findViewById(INTERNAL_EMPTY_ID);
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mStandardEmptyView == null) {
303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mEmptyView = root.findViewById(android.R.id.empty);
3049c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            } else {
3059c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mStandardEmptyView.setVisibility(View.GONE);
306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mProgressContainer = null; //root.findViewById(com.android.internal.R.id.progressContainer);
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mListContainer = null; //root.findViewById(com.android.internal.R.id.listContainer);
309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            View rawListView = root.findViewById(android.R.id.list);
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (!(rawListView instanceof ListView)) {
311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (rawListView == null) {
312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    throw new RuntimeException(
313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            "Your content must have a ListView whose id attribute is " +
314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            "'android.R.id.list'");
315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                throw new RuntimeException(
317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        "Content has view with id attribute 'android.R.id.list' "
318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        + "that is not a ListView class");
319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mList = (ListView)rawListView;
321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mEmptyView != null) {
322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mList.setEmptyView(mEmptyView);
3239c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            } else if (mEmptyText != null) {
3249c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mStandardEmptyView.setText(mEmptyText);
3259c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                mList.setEmptyView(mStandardEmptyView);
326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mListShown = true;
329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mList.setOnItemClickListener(mOnClickListener);
330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAdapter != null) {
3319c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            ListAdapter adapter = mAdapter;
3329c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            mAdapter = null;
3339c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            setListAdapter(adapter);
334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // We are starting without an adapter, so assume we won't
336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // have our data right away and start with the progress indicator.
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mProgressContainer != null) {
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                setListShown(false, false);
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mHandler.post(mRequestFocus);
342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
344