Make FramementLayout demo better... and, um actually work.

Also some other small cleanup.

Change-Id: Iaaf5d4a85113b4a01a4bd3986e2334972e1096ad
This commit is contained in:
Dianne Hackborn
2010-09-12 19:38:56 -07:00
parent dc42f58a69
commit 8fc1811242
5 changed files with 43 additions and 17 deletions

View File

@@ -26,8 +26,8 @@
android:id="@+id/titles" android:layout_weight="1" android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" /> android:layout_width="0px" android:layout_height="match_parent" />
<fragment class="com.example.android.apis.app.FragmentLayout$DialogFragment" <fragment class="com.example.android.apis.app.FragmentLayout$DetailsFragment"
android:id="@+id/dialog" android:layout_weight="1" android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" /> android:layout_width="0px" android:layout_height="match_parent" />
</LinearLayout> </LinearLayout>

View File

@@ -20,8 +20,7 @@
<!-- BEGIN_INCLUDE(layout) --> <!-- BEGIN_INCLUDE(layout) -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
<fragment <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles" android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" /> android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout> </FrameLayout>

View File

@@ -26,10 +26,10 @@ import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ScrollView; import android.widget.ScrollView;
@@ -82,29 +82,52 @@ public class FragmentLayout extends Activity {
//BEGIN_INCLUDE(titles) //BEGIN_INCLUDE(titles)
public static class TitlesFragment extends ListFragment { public static class TitlesFragment extends ListFragment {
DetailsFragment mDetails;
int mCurCheckPosition = 0;
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles. // Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(), setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Shakespeare.TITLES)); android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
// Restore last state for checked position.
if (savedInstanceState != null) {
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
// If we are showing details in the screen, set up the list to highlight.
mDetails = (DetailsFragment)getFragmentManager().findFragmentById(R.id.details);
if (mDetails != null && mDetails.isInLayout()) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(mCurCheckPosition, true);
mDetails.setText(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
} }
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
DetailsFragment frag = (DetailsFragment) mCurCheckPosition = position;
getFragmentManager().findFragmentById(R.id.dialog);
if (frag != null && frag.isVisible()) { if (mDetails != null && mDetails.isVisible()) {
// If the activity has a fragment to display the dialog, // If the activity has a fragment to display the dialog,
// point it to what the user has selected. // point it to what the user has selected.
frag.setText((int)id); mDetails.setText(position);
getListView().setItemChecked(position, true);
} else { } else {
// Otherwise we need to launch a new activity to display // Otherwise we need to launch a new activity to display
// the dialog fragment with selected text. // the dialog fragment with selected text.
Intent intent = new Intent(); Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class); intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("text", (int)id); intent.putExtra("text", position);
startActivity(intent); startActivity(intent);
} }
} }
@@ -142,8 +165,13 @@ public class FragmentLayout extends Activity {
Bundle savedInstanceState) { Bundle savedInstanceState) {
ScrollView scroller = new ScrollView(getActivity()); ScrollView scroller = new ScrollView(getActivity());
mText = new TextView(getActivity()); mText = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
mText.setPadding(padding, padding, padding, padding);
scroller.addView(mText); scroller.addView(mText);
setText(mDisplayedText); if (mDisplayedText >= 0) {
mText.setText(Shakespeare.DIALOGUE[mDisplayedText]);
}
return scroller; return scroller;
} }
} }

View File

@@ -63,9 +63,7 @@ public class FragmentRetainInstance extends Activity {
Button button = (Button)v.findViewById(R.id.restart); Button button = (Button)v.findViewById(R.id.restart);
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
if (mWorkFragment != null) { mWorkFragment.restart();
mWorkFragment.restart();
}
} }
}); });

View File

@@ -29,7 +29,8 @@ import android.widget.Toast;
/** /**
* This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView * This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView
* couple with the new simple_selectable_list_item which uses a highligted border for selected items. * couple with the new simple_list_item_activated_1 which uses a highlighted border for selected
* items.
*/ */
public class List16 extends ListActivity { public class List16 extends ListActivity {
@Override @Override
@@ -39,7 +40,7 @@ public class List16 extends ListActivity {
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new ModeCallback()); lv.setMultiChoiceModeListener(new ModeCallback());
setListAdapter(new ArrayAdapter<String>(this, setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_selectable_list_item, mStrings)); android.R.layout.simple_list_item_activated_1, mStrings));
} }
@Override @Override