New fragment sample.

This shows more complicated list interaction: using a loader
to populate the list with results from a cursor.  Makes use
of the new built-in empty message and indeterminant progress
features.

Change-Id: I04afb50879995bade286aab0eedfff40f6a5e304
This commit is contained in:
Dianne Hackborn
2010-06-25 17:15:39 -07:00
parent 5b836a44c7
commit 3c68928dd8
4 changed files with 156 additions and 3 deletions

View File

@@ -216,6 +216,13 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentComplexList" android:label="@string/fragment_complex_list">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.FragmentContextMenu" <activity android:name=".app.FragmentContextMenu"
android:label="@string/fragment_context_menu"> android:label="@string/fragment_context_menu">
<intent-filter> <intent-filter>

View File

@@ -85,6 +85,8 @@
<string name="fragment_anim">App/Fragment/Anim</string> <string name="fragment_anim">App/Fragment/Anim</string>
<string name="fragment_complex_list">App/Fragment/Complex List</string>
<string name="fragment_context_menu">App/Fragment/Context Menu</string> <string name="fragment_context_menu">App/Fragment/Context Menu</string>
<string name="fragment_context_menu_msg">Fragment populating a context <string name="fragment_context_menu_msg">Fragment populating a context
menu; long press the button to see.</string> menu; long press the button to see.</string>

View File

@@ -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<Cursor> {
ExampleComplexListFragment mListFragment;
void setListFragment(ExampleComplexListFragment fragment) {
mListFragment = fragment;
}
@Override
protected Loader<Cursor> 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<Cursor> 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<Cursor> 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<Cursor> 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();
}
}
}

View File

@@ -16,7 +16,6 @@
package com.example.android.apis.app; package com.example.android.apis.app;
import com.example.android.apis.R;
import com.example.android.apis.Shakespeare; import com.example.android.apis.Shakespeare;
import android.app.Activity; import android.app.Activity;
@@ -36,8 +35,10 @@ public class FragmentList extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content. // Create the list fragment and add it as our sole content.
ExampleListFragment list = new ExampleListFragment(); if (findFragmentById(android.R.id.content) == null) {
openFragmentTransaction().add(android.R.id.content, list).commit(); ExampleListFragment list = new ExampleListFragment();
openFragmentTransaction().add(android.R.id.content, list).commit();
}
} }
public static class ExampleListFragment extends ListFragment { public static class ExampleListFragment extends ListFragment {