diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 3e22eb54e..80fa62bb1 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -216,6 +216,13 @@ + + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 6d58cc364..20a8fef41 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -85,6 +85,8 @@ App/Fragment/Anim + App/Fragment/Complex List + App/Fragment/Context Menu Fragment populating a context menu; long press the button to see. diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentComplexList.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentComplexList.java new file mode 100644 index 000000000..29ae8f827 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentComplexList.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.apis.app; + +import com.example.android.apis.Shakespeare; + +import android.app.Activity; +import android.app.Fragment; +import android.app.ListFragment; +import android.app.LoaderManagingFragment; +import android.content.Context; +import android.content.CursorLoader; +import android.content.Loader; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Contacts.Phones; +import android.util.Log; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.SimpleCursorAdapter; + +/** + * Demonstration of more complex use if a ListFragment, including showing + * an empty view and loading progress. + */ +public class FragmentComplexList extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Create the list fragment and add it as our sole content. + if (findFragmentById(android.R.id.content) == null) { + ExampleComplexListFragment list = new ExampleComplexListFragment(); + openFragmentTransaction().add(android.R.id.content, list).commit(); + } + } + + public static class ContactsLoader extends LoaderManagingFragment { + ExampleComplexListFragment mListFragment; + + void setListFragment(ExampleComplexListFragment fragment) { + mListFragment = fragment; + } + + @Override + protected Loader onCreateLoader(int id, Bundle args) { + return new CursorLoader(getActivity(), Phones.CONTENT_URI, null, null, null, null); + } + + @Override + protected void onInitializeLoaders() { + } + + @Override + protected void onLoadFinished(Loader loader, Cursor data) { + mListFragment.loadFinished(loader, data); + } + } + + public static class ExampleComplexListFragment extends ListFragment { + ContactsLoader mLoader; + boolean mInitializing; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mInitializing = true; + + // Make sure we have the loader for our content. + mLoader = (ContactsLoader)getActivity().findFragmentByTag( + ContactsLoader.class.getName()); + if (mLoader == null) { + mLoader = new ContactsLoader(); + getActivity().openFragmentTransaction().add(mLoader, + ContactsLoader.class.getName()).commit(); + } + mLoader.setListFragment(this); + } + + @Override + public void onReady(Bundle savedInstanceState) { + super.onReady(savedInstanceState); + + // Assume we don't yet have data to display. + setListShown(false, false); + + // Give some text to display if there is no data. In a real + // application this would come from a resource. + setEmptyText("No phone numbers"); + + // Check if we already have content for the list. + Loader ld = mLoader.getLoader(0); + if (ld == null) { + // No loader started yet... do it now. + ld = mLoader.startLoading(0, null); + } else { + // Already have a loader -- poke it to report its cursor + // if it already has one. This will immediately call back + // to us for us to update the list right now. + ld.startLoading(); + } + mInitializing = false; + } + + void loadFinished(Loader loader, Cursor data) { + ListAdapter adapter = new SimpleCursorAdapter(getActivity(), + android.R.layout.simple_list_item_2, data, + new String[] { Phones.NAME, Phones.NUMBER }, + new int[] { android.R.id.text1, android.R.id.text2 }); + setListAdapter(adapter); + setListShown(true, !mInitializing); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + Log.i("FragmentComplexList", "Item clicked: " + id); + } + + @Override + public void onDestroy() { + mLoader.setListFragment(this); + super.onDestroy(); + } + } +} diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java index 976bcc144..a636f449b 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java @@ -16,7 +16,6 @@ package com.example.android.apis.app; -import com.example.android.apis.R; import com.example.android.apis.Shakespeare; import android.app.Activity; @@ -36,8 +35,10 @@ public class FragmentList extends Activity { super.onCreate(savedInstanceState); // Create the list fragment and add it as our sole content. - ExampleListFragment list = new ExampleListFragment(); - openFragmentTransaction().add(android.R.id.content, list).commit(); + if (findFragmentById(android.R.id.content) == null) { + ExampleListFragment list = new ExampleListFragment(); + openFragmentTransaction().add(android.R.id.content, list).commit(); + } } public static class ExampleListFragment extends ListFragment {