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 19f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackbornimport android.content.Context; 20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle; 21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler; 22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Gravity; 23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.LayoutInflater; 24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View; 25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup; 26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.AnimationUtils; 27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.AdapterView; 28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.FrameLayout; 29f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackbornimport android.widget.LinearLayout; 30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ListAdapter; 31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ListView; 32f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackbornimport android.widget.ProgressBar; 33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.TextView; 34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.ListFragment}. 37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0. When running 38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try 39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation. See the framework SDK 40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview. 41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class ListFragment extends Fragment { 43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final int INTERNAL_EMPTY_ID = 0x00ff0001; 44f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002; 45f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003; 46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final private Handler mHandler = new Handler(); 48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final private Runnable mRequestFocus = new Runnable() { 50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void run() { 51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.focusableViewAvailable(mList); 52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final private AdapterView.OnItemClickListener mOnClickListener 56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn = new AdapterView.OnItemClickListener() { 57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn onListItemClick((ListView)parent, v, position, id); 59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ListAdapter mAdapter; 63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ListView mList; 64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View mEmptyView; 65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn TextView mStandardEmptyView; 66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View mProgressContainer; 67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View mListContainer; 689c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn CharSequence mEmptyText; 69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mListShown; 70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ListFragment() { 72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Provide default implementation to return a simple list view. Subclasses 76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * can override to replace with their own layout. If doing so, the 77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returned view hierarchy <em>must</em> have a ListView whose id 78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is {@link android.R.id#list android.R.id.list} and can optionally 79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * have a sibling view id {@link android.R.id#empty android.R.id.empty} 80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * that is to be shown when the list is empty. 81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>If you are overriding this method with your own custom content, 83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * consider including the standard layout {@link android.R.layout#list_content} 84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * in your layout file, so that you continue to retain all of the standard 85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * behavior of ListFragment. In particular, this is currently the only 86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * way to have the built-in indeterminant progress state be shown. 87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public View onCreateView(LayoutInflater inflater, ViewGroup container, 90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Bundle savedInstanceState) { 91f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn final Context context = getActivity(); 92f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 93f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn FrameLayout root = new FrameLayout(context); 94f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 95f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn // ------------------------------------------------------------------ 96f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 97f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn LinearLayout pframe = new LinearLayout(context); 98f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID); 99f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn pframe.setOrientation(LinearLayout.VERTICAL); 100f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn pframe.setVisibility(View.GONE); 101f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn pframe.setGravity(Gravity.CENTER); 102f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 103f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn ProgressBar progress = new ProgressBar(context, null, 104f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn android.R.attr.progressBarStyleLarge); 105f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn pframe.addView(progress, new FrameLayout.LayoutParams( 106f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 107f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 108f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn root.addView(pframe, new FrameLayout.LayoutParams( 109f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 110f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 111f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn // ------------------------------------------------------------------ 112f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 113f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn FrameLayout lframe = new FrameLayout(context); 114f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn lframe.setId(INTERNAL_LIST_CONTAINER_ID); 115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn TextView tv = new TextView(getActivity()); 117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn tv.setId(INTERNAL_EMPTY_ID); 118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn tv.setGravity(Gravity.CENTER); 119f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn lframe.addView(tv, new FrameLayout.LayoutParams( 120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ListView lv = new ListView(getActivity()); 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn lv.setId(android.R.id.list); 124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn lv.setDrawSelectorOnTop(false); 125f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn lframe.addView(lv, new FrameLayout.LayoutParams( 126f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 127f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 128f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn root.addView(lframe, new FrameLayout.LayoutParams( 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 131f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn // ------------------------------------------------------------------ 132f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn 133f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn root.setLayoutParams(new FrameLayout.LayoutParams( 134f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return root; 137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 140e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn * Attach to list view once the view hierarchy has been created. 141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 143e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn public void onViewCreated(View view, Bundle savedInstanceState) { 144e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn super.onViewCreated(view, savedInstanceState); 145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Detach from list view. 150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onDestroyView() { 153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mHandler.removeCallbacks(mRequestFocus); 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList = null; 1559c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mListShown = false; 1569c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mEmptyView = mProgressContainer = mListContainer = null; 1579c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mStandardEmptyView = null; 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onDestroyView(); 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This method will be called when an item in the list is selected. 163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Subclasses should override. Subclasses can call 164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * getListView().getItemAtPosition(position) if they need to access the 165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * data associated with the selected item. 166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param l The ListView where the click happened 168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param v The view that was clicked within the ListView 169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param position The position of the view in the list 170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param id The row id of the item that was clicked 171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onListItemClick(ListView l, View v, int position, long id) { 173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Provide the cursor for the list view. 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListAdapter(ListAdapter adapter) { 179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean hadAdapter = mAdapter != null; 180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdapter = adapter; 181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mList != null) { 182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setAdapter(adapter); 183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!mListShown && !hadAdapter) { 184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The list was hidden, and previously didn't have an 185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // adapter. It is now time to show it. 186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(true, getView().getWindowToken() != null); 187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Set the currently selected list item to the specified 193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * position with the adapter's data 194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param position 196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setSelection(int position) { 198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setSelection(position); 200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the position of the currently selected list item. 204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getSelectedItemPosition() { 206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList.getSelectedItemPosition(); 208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the cursor row ID of the currently selected list item. 212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public long getSelectedItemId() { 214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList.getSelectedItemId(); 216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the activity's list view widget. 220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ListView getListView() { 222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mList; 224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * The default content for a ListFragment has a TextView that can 228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * be shown when the list is empty. If you would like to have it 229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * shown, call this method to supply the text it should use. 230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setEmptyText(CharSequence text) { 232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStandardEmptyView == null) { 234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Can't be used with a custom content view"); 235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStandardEmptyView.setText(text); 2379c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn if (mEmptyText == null) { 238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setEmptyView(mStandardEmptyView); 239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2409c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mEmptyText = text; 241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the list is being displayed. You can make it not 245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * displayed if you are waiting for the initial data to show in it. During 246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this time an indeterminant progress indicator will be shown instead. 247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Applications do not normally need to use this themselves. The default 249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * behavior of ListFragment is to start with the list not being shown, only 250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * showing it once an adapter is given with {@link #setListAdapter(ListAdapter)}. 251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * If the list at that point had not been shown, when it does get shown 252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * it will be do without the user ever seeing the hidden state. 253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param shown If true, the list view is shown; if false, the progress 255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * indicator. The initial value is true. 256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListShown(boolean shown) { 258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(shown, true); 259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #setListShown(boolean)}, but no animation is used when 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * transitioning from the previous state. 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setListShownNoAnimation(boolean shown) { 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(shown, false); 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the list is being displayed. You can make it not 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * displayed if you are waiting for the initial data to show in it. During 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this time an indeterminant progress indicator will be shown instead. 273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param shown If true, the list view is shown; if false, the progress 275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * indicator. The initial value is true. 276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param animate If true, an animation will be used to transition to the 277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * new state. 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void setListShown(boolean shown, boolean animate) { 280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ensureList(); 281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mProgressContainer == null) { 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Can't be used with a custom content view"); 283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mListShown == shown) { 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListShown = shown; 288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (shown) { 289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animate) { 290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.startAnimation(AnimationUtils.loadAnimation( 291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_out)); 292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.startAnimation(AnimationUtils.loadAnimation( 293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_in)); 2949c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn } else { 2959c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mProgressContainer.clearAnimation(); 2969c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mListContainer.clearAnimation(); 297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.setVisibility(View.GONE); 299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.setVisibility(View.VISIBLE); 300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animate) { 302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.startAnimation(AnimationUtils.loadAnimation( 303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_in)); 304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.startAnimation(AnimationUtils.loadAnimation( 305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn getActivity(), android.R.anim.fade_out)); 3069c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn } else { 3079c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mProgressContainer.clearAnimation(); 3089c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mListContainer.clearAnimation(); 309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mProgressContainer.setVisibility(View.VISIBLE); 311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListContainer.setVisibility(View.GONE); 312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Get the ListAdapter associated with this activity's ListView. 317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ListAdapter getListAdapter() { 319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mAdapter; 320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void ensureList() { 323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mList != null) { 324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View root = getView(); 327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (root == null) { 328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Content view not yet created"); 329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (root instanceof ListView) { 331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList = (ListView)root; 332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStandardEmptyView = (TextView)root.findViewById(INTERNAL_EMPTY_ID); 334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStandardEmptyView == null) { 335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mEmptyView = root.findViewById(android.R.id.empty); 3369c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn } else { 3379c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mStandardEmptyView.setVisibility(View.GONE); 338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 339f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn mProgressContainer = root.findViewById(INTERNAL_PROGRESS_CONTAINER_ID); 340f81fd8c39e5ca44a4adb2fb45f9b382305842d87Dianne Hackborn mListContainer = root.findViewById(INTERNAL_LIST_CONTAINER_ID); 341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View rawListView = root.findViewById(android.R.id.list); 342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!(rawListView instanceof ListView)) { 343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (rawListView == null) { 344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new RuntimeException( 345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Your content must have a ListView whose id attribute is " + 346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "'android.R.id.list'"); 347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new RuntimeException( 349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Content has view with id attribute 'android.R.id.list' " 350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + "that is not a ListView class"); 351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList = (ListView)rawListView; 353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mEmptyView != null) { 354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setEmptyView(mEmptyView); 3559c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn } else if (mEmptyText != null) { 3569c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mStandardEmptyView.setText(mEmptyText); 3579c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mList.setEmptyView(mStandardEmptyView); 358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mListShown = true; 361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mList.setOnItemClickListener(mOnClickListener); 362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdapter != null) { 3639c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn ListAdapter adapter = mAdapter; 3649c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn mAdapter = null; 3659c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn setListAdapter(adapter); 366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // We are starting without an adapter, so assume we won't 368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // have our data right away and start with the progress indicator. 369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mProgressContainer != null) { 370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setListShown(false, false); 371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mHandler.post(mRequestFocus); 374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 376