Cleanup fragment api demos

* Cleaned up extra whitespace & made vertical whitespace more consistent
* 100col wrap
* Remove unused imports
* Add @Override where necessary

Change-Id: I1ad404ffc4ce7fa9c4b661f7cc1783c86356316b
This commit is contained in:
Andrew Stadler
2010-08-03 17:39:20 -07:00
parent 132ab286a1
commit 45ae9c3def
12 changed files with 89 additions and 75 deletions

View File

@@ -30,6 +30,7 @@ import android.widget.Button;
import android.widget.TextView;
public class FragmentAlertDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -53,21 +54,26 @@ public class FragmentAlertDialog extends Activity {
}
public static class MyAlertDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
}
})
)
.create();
}
}

View File

@@ -34,15 +34,16 @@ import android.widget.TextView;
* Demonstration of animations when changing fragment states.
*/
public class FragmentAnim extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_anim);
addShowHideListener(R.id.frag1hide, findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, findFragmentById(R.id.fragment2));
}
void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findViewById(buttonId);
button.setOnClickListener(new OnClickListener() {
@@ -62,16 +63,17 @@ public class FragmentAnim extends Activity {
}
});
}
public static class FirstFragment extends Fragment {
TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The fragment saves and restores this text.");
// Retrieve the text editor, and restore the last saved state if needed.
mTextView = (TextView)v.findViewById(R.id.saved);
if (savedInstanceState != null) {
@@ -83,19 +85,21 @@ public class FragmentAnim extends Activity {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Remember the current text, to restore if we later restart.
outState.putCharSequence("text", mTextView.getText());
}
}
public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The TextView saves and restores this text.");
// Retrieve the text editor and tell it to save and restore its state.
// Note that you will often set this in the layout XML, but since
// we are sharing our layout with the other fragment we will customize

View File

@@ -34,16 +34,18 @@ import android.view.ContextMenu.ContextMenuInfo;
* Demonstration of displaying a context menu from a fragment.
*/
public class FragmentContextMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content.
ContextMenuFragment content = new ContextMenuFragment();
openFragmentTransaction().add(android.R.id.content, content).commit();
}
public static class ContextMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@@ -58,7 +60,7 @@ public class FragmentContextMenu extends Activity {
menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A");
menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {

View File

@@ -132,6 +132,7 @@ public class FragmentDialog extends Activity {
outState.putInt("num", mNum);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);

View File

@@ -21,18 +21,14 @@ import com.example.android.apis.Shakespeare;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
@@ -43,6 +39,7 @@ import android.widget.TextView;
* landscape.
*/
public class FragmentLayout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -50,16 +47,17 @@ public class FragmentLayout extends Activity {
// This layout varies depending on the screen size.
setContentView(R.layout.fragment_layout);
}
/**
* This is a secondary activity, to show what the user has selected
* when the screen is not large enough to show it all in one activity.
*/
public static class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
@@ -67,15 +65,16 @@ public class FragmentLayout extends Activity {
finish();
return;
}
DialogFragment dialog = new DialogFragment();
openFragmentTransaction().add(android.R.id.content, dialog).commit();
dialog.setText(getIntent().getIntExtra("text", -1));
}
}
public static class TitlesFragment extends Fragment
implements AdapterView.OnItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView list = new ListView(getActivity());
@@ -99,18 +98,18 @@ public class FragmentLayout extends Activity {
}
}
}
public static class DialogFragment extends Fragment {
int mDisplayedText = -1;
TextView mText;
public void setText(int id) {
mDisplayedText = id;
if (mText != null && id >= 0) {
mText.setText(Shakespeare.DIALOGUE[id]);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -125,6 +124,7 @@ public class FragmentLayout extends Activity {
outState.putInt("text", mDisplayedText);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ScrollView scroller = new ScrollView(getActivity());

View File

@@ -31,25 +31,27 @@ import android.widget.ListView;
* from a canned array.
*/
public class FragmentListArray 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) {
ArrayListFragment list = new ArrayListFragment();
openFragmentTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Shakespeare.TITLES));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);

View File

@@ -23,7 +23,6 @@ import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
@@ -36,6 +35,7 @@ import android.widget.SimpleCursorAdapter;
* an empty view and loading progress.
*/
public class FragmentListCursorLoader extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -46,7 +46,7 @@ public class FragmentListCursorLoader extends Activity {
openFragmentTransaction().add(android.R.id.content, list).commit();
}
}
public static class CursorLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
@@ -61,12 +61,12 @@ public class FragmentListCursorLoader extends Activity {
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentComplexList", "Item clicked: " + id);
}
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
@@ -75,7 +75,7 @@ public class FragmentListCursorLoader extends Activity {
Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY,
};
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("

View File

@@ -6,15 +6,11 @@ import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* Demonstrates how fragments can participate in the options menu.
@@ -24,14 +20,14 @@ public class FragmentMenu extends Activity {
Fragment mFragment2;
CheckBox mCheckBox1;
CheckBox mCheckBox2;
// Update fragment visibility when check boxes are changed.
final OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
updateFragmentVisibility();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -60,7 +56,7 @@ public class FragmentMenu extends Activity {
// Make sure fragments start out with correct visibility.
updateFragmentVisibility();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
@@ -77,13 +73,14 @@ public class FragmentMenu extends Activity {
else ft.hide(mFragment2);
ft.commit();
}
/**
* A fragment that displays a menu. This fragment happens to not
* have a UI (it does not implement onCreateView), but it could also
* have one if it wanted.
*/
public static class MenuFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -101,6 +98,7 @@ public class FragmentMenu extends Activity {
* Second fragment with a menu.
*/
public static class Menu2Fragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@@ -1,7 +1,6 @@
package com.example.android.apis.app;
import com.example.android.apis.R;
import com.example.android.apis.app.FragmentStack.CountingFragment;
import android.app.Activity;
import android.app.Fragment;
@@ -11,13 +10,14 @@ import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
public class FragmentReceiveResult extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -35,13 +35,13 @@ public class FragmentReceiveResult extends Activity {
ft.add(R.id.simple_fragment, newFragment).commit();
}
}
public static class ReceiveResultFragment extends Fragment {
// Definition of the one requestCode we use for receiving resuls.
static final private int GET_CODE = 0;
private TextView mResults;
private OnClickListener mGetListener = new OnClickListener() {
public void onClick(View v) {
// Start the activity whose result we want to retrieve. The
@@ -61,6 +61,7 @@ public class FragmentReceiveResult extends Activity {
super.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.receive_result, container, false);
@@ -77,7 +78,7 @@ public class FragmentReceiveResult extends Activity {
return v;
}
/**
* This method is called when the sending activity has finished, with the
* result it supplied.
@@ -112,6 +113,5 @@ public class FragmentReceiveResult extends Activity {
text.append("\n");
}
}
}
}

View File

@@ -20,15 +20,11 @@ import com.example.android.apis.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* This example shows how you can use a Fragment to easily propagate state
@@ -38,12 +34,12 @@ import android.widget.TextView;
*/
public class FragmentRetainInstance extends Activity {
RetainedFragment mRetainedFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_retain_instance);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.restart);
button.setOnClickListener(new OnClickListener() {
@@ -51,17 +47,17 @@ public class FragmentRetainInstance extends Activity {
mRetainedFragment.restart();
}
});
// Check to see if we retained the fragment.
mRetainedFragment = (RetainedFragment)findFragmentByTag("retained");
// If not retained (or first time running), we need to re-create it.
if (mRetainedFragment == null) {
mRetainedFragment = new RetainedFragment();
openFragmentTransaction().add(mRetainedFragment, "retained").commit();
}
}
/**
* This is the Fragment implementation that will be retained across
* activity instances. It represents some ongoing work, here a thread
@@ -72,7 +68,7 @@ public class FragmentRetainInstance extends Activity {
int mPosition;
boolean mReady = false;
boolean mQuiting = false;
/**
* This is the thread that will do our work. It sits in a loop running
* the progress up until it has reached the top, then stops and waits.
@@ -82,10 +78,10 @@ public class FragmentRetainInstance extends Activity {
public void run() {
// We'll figure the real value out later.
int max = 10000;
// This thread runs almost forever.
while (true) {
// Update our shared state with the UI.
synchronized (this) {
// Our thread is stopped if the UI is not ready
@@ -99,7 +95,7 @@ public class FragmentRetainInstance extends Activity {
} catch (InterruptedException e) {
}
}
// Now update the progress. Note it is important that
// we touch the progress bar with the lock held, so it
// doesn't disappear on us.
@@ -107,7 +103,7 @@ public class FragmentRetainInstance extends Activity {
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);
}
// Normally we would be doing some work, but put a kludge
// here to pretend like we are.
synchronized (this) {
@@ -119,7 +115,7 @@ public class FragmentRetainInstance extends Activity {
}
}
};
/**
* Fragment initialization. We way we want to be retained and
* start our thread.
@@ -190,7 +186,7 @@ public class FragmentRetainInstance extends Activity {
super.onDetach();
}
/**
* API for our UI to restart the progress thread.
*/

View File

@@ -31,7 +31,7 @@ import android.widget.TextView;
public class FragmentStack extends Activity {
int mStackLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -54,7 +54,7 @@ public class FragmentStack extends Activity {
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
@@ -70,18 +70,18 @@ public class FragmentStack extends Activity {
ft.addToBackStack(null);
ft.commit();
}
public static class CountingFragment extends Fragment {
int mNum;
public CountingFragment() {
mNum = -1;
}
public CountingFragment(int num) {
mNum = num;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -96,6 +96,7 @@ public class FragmentStack extends Activity {
outState.putInt("num", mNum);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);

View File

@@ -33,6 +33,7 @@ import android.widget.ListView;
*/
//BEGIN_INCLUDE(activity)
public class FragmentPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -47,6 +48,7 @@ public class FragmentPreferences extends Activity {
* pick one, the corresponding preferences fragment is created and shown.
*/
public static class CategoriesFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -72,6 +74,7 @@ public class FragmentPreferences extends Activity {
//BEGIN_INCLUDE(fragment)
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -83,6 +86,7 @@ public class FragmentPreferences extends Activity {
//END_INCLUDE(fragment)
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);