ListFragment.java revision cba2e2c881e8e16ea5025b564c94320174d65f01
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; 63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mSetEmptyText; 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 /** 108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Attach to list view once Fragment is ready to run. 109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onActivityCreated(Bundle savedInstanceState) { 112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onActivityCreated(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; 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onDestroyView(); 124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This method will be called when an item in the list is selected. 128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Subclasses should override. Subclasses can call 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * getListView().getItemAtPosition(position) if they need to access the 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * data associated with the selected item. 131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param l The ListView where the click happened 133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param v The view that was clicked within the ListView 134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param position The position of the view in the list 135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param id The row id of the item that was clicked 136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onListItemClick(ListView l, View v, int position, long id) { 138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Provide the cursor for the list view. 142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListAdapter(ListAdapter adapter) { 144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean hadAdapter = mAdapter != null; 145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdapter = adapter; 146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mList != null) { 147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setAdapter(adapter); 148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!mListShown && !hadAdapter) { 149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The list was hidden, and previously didn't have an 150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // adapter. It is now time to show it. 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(true, getView().getWindowToken() != null); 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Set the currently selected list item to the specified 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * position with the adapter's data 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param position 161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setSelection(int position) { 163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setSelection(position); 165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the position of the currently selected list item. 169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getSelectedItemPosition() { 171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList.getSelectedItemPosition(); 173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the cursor row ID of the currently selected list item. 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public long getSelectedItemId() { 179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList.getSelectedItemId(); 181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the activity's list view widget. 185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ListView getListView() { 187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList; 189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * The default content for a ListFragment has a TextView that can 193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * be shown when the list is empty. If you would like to have it 194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * shown, call this method to supply the text it should use. 195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setEmptyText(CharSequence text) { 197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStandardEmptyView == null) { 199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Can't be used with a custom content view"); 200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStandardEmptyView.setText(text); 202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!mSetEmptyText) { 203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setEmptyView(mStandardEmptyView); 204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mSetEmptyText = true; 205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the list is being displayed. You can make it not 210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * displayed if you are waiting for the initial data to show in it. During 211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this time an indeterminant progress indicator will be shown instead. 212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Applications do not normally need to use this themselves. The default 214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * behavior of ListFragment is to start with the list not being shown, only 215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * showing it once an adapter is given with {@link #setListAdapter(ListAdapter)}. 216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * If the list at that point had not been shown, when it does get shown 217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * it will be do without the user ever seeing the hidden state. 218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param shown If true, the list view is shown; if false, the progress 220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * indicator. The initial value is true. 221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListShown(boolean shown) { 223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(shown, true); 224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #setListShown(boolean)}, but no animation is used when 228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * transitioning from the previous state. 229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListShownNoAnimation(boolean shown) { 231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(shown, false); 232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the list is being displayed. You can make it not 236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * displayed if you are waiting for the initial data to show in it. During 237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this time an indeterminant progress indicator will be shown instead. 238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param shown If true, the list view is shown; if false, the progress 240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * indicator. The initial value is true. 241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param animate If true, an animation will be used to transition to the 242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * new state. 243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void setListShown(boolean shown, boolean animate) { 245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mProgressContainer == null) { 247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Can't be used with a custom content view"); 248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mListShown == shown) { 250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListShown = shown; 253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (shown) { 254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animate) { 255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.startAnimation(AnimationUtils.loadAnimation( 256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_out)); 257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.startAnimation(AnimationUtils.loadAnimation( 258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_in)); 259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.setVisibility(View.GONE); 261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.setVisibility(View.VISIBLE); 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animate) { 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.startAnimation(AnimationUtils.loadAnimation( 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_in)); 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.startAnimation(AnimationUtils.loadAnimation( 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_out)); 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.setVisibility(View.VISIBLE); 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.setVisibility(View.GONE); 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the ListAdapter associated with this activity's ListView. 276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ListAdapter getListAdapter() { 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mAdapter; 279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void ensureList() { 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mList != null) { 283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View root = getView(); 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (root == null) { 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Content view not yet created"); 288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (root instanceof ListView) { 290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList = (ListView)root; 291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStandardEmptyView = (TextView)root.findViewById(INTERNAL_EMPTY_ID); 293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStandardEmptyView == null) { 294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mEmptyView = root.findViewById(android.R.id.empty); 295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer = null; //root.findViewById(com.android.internal.R.id.progressContainer); 297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer = null; //root.findViewById(com.android.internal.R.id.listContainer); 298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View rawListView = root.findViewById(android.R.id.list); 299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!(rawListView instanceof ListView)) { 300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (rawListView == null) { 301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new RuntimeException( 302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Your content must have a ListView whose id attribute is " + 303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "'android.R.id.list'"); 304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new RuntimeException( 306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Content has view with id attribute 'android.R.id.list' " 307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + "that is not a ListView class"); 308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList = (ListView)rawListView; 310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mEmptyView != null) { 311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setEmptyView(mEmptyView); 312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListShown = true; 315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setOnItemClickListener(mOnClickListener); 316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdapter != null) { 317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListAdapter(mAdapter); 318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // We are starting without an adapter, so assume we won't 320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // have our data right away and start with the progress indicator. 321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mProgressContainer != null) { 322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(false, false); 323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mHandler.post(mRequestFocus); 326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 328