diff options
author | Dake Gu <dake@google.com> | 2015-09-21 17:55:59 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-09-21 17:55:59 +0000 |
commit | fc169782398aba5fd6784ecfa3bed72b12e72de2 (patch) | |
tree | 2028192defb86ed1195e8d6724aa187896fe71a1 | |
parent | 70e495401e29ff89e1e385449dce02d4b0530ecb (diff) | |
parent | 7b42f57cdaf6b4176414a75268af1087169b1090 (diff) | |
download | android_development-fc169782398aba5fd6784ecfa3bed72b12e72de2.tar.gz android_development-fc169782398aba5fd6784ecfa3bed72b12e72de2.tar.bz2 android_development-fc169782398aba5fd6784ecfa3bed72b12e72de2.zip |
Merge "Add example of vertical grid transition and GuidedStepFragment" into mnc-ub-dev
8 files changed, 80 insertions, 35 deletions
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml index e382ed91d..9b29b022b 100644 --- a/samples/SupportLeanbackDemos/AndroidManifest.xml +++ b/samples/SupportLeanbackDemos/AndroidManifest.xml @@ -45,12 +45,14 @@ android:exported="true" /> <activity android:name="VerticalGridActivity" + android:theme="@style/Theme.Example.Leanback.VerticalGrid" android:exported="true" /> <activity android:name="SearchActivity" android:exported="true" /> <activity android:name="BrowseErrorActivity" + android:theme="@style/Theme.Example.Leanback.Browse" android:exported="true" /> <activity android:name="HorizontalGridTestActivity" diff --git a/samples/SupportLeanbackDemos/res/layout/browse.xml b/samples/SupportLeanbackDemos/res/layout/browse.xml index 367337aca..4d46233ce 100644 --- a/samples/SupportLeanbackDemos/res/layout/browse.xml +++ b/samples/SupportLeanbackDemos/res/layout/browse.xml @@ -27,4 +27,9 @@ android:layout_height="match_parent" /> + <!-- container for hosting GuidedStepFragment background --> + <FrameLayout android:id="@+id/lb_guidedstep_background" + android:layout_width="match_parent" + android:layout_height="match_parent" /> + </FrameLayout> diff --git a/samples/SupportLeanbackDemos/res/values/themes.xml b/samples/SupportLeanbackDemos/res/values/themes.xml index 696d82c4b..1fdf46bf5 100644 --- a/samples/SupportLeanbackDemos/res/values/themes.xml +++ b/samples/SupportLeanbackDemos/res/values/themes.xml @@ -23,6 +23,8 @@ </style> <style name="Theme.Example.Leanback.Details" parent="Theme.Leanback.Details"> </style> + <style name="Theme.Example.Leanback.VerticalGrid" parent="Theme.Leanback.VerticalGrid"> + </style> <style name="Theme.Example.Leanback.Rows" parent="Theme.Leanback"> <item name="browseTitleTextStyle">@style/Widget.Example.Leanback.Title.Text</item> <item name="rowsVerticalGridStyle">@style/Widget.Example.Leanback.Rows.VerticalGridView</item> diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java index 0a509511d..f4c8044b0 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java @@ -17,6 +17,7 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.ActivityOptionsCompat; +import android.support.v17.leanback.app.GuidedStepFragment; import android.support.v17.leanback.widget.ArrayObjectAdapter; import android.support.v17.leanback.widget.HeaderItem; import android.support.v17.leanback.widget.ImageCardView; @@ -42,6 +43,10 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm private ArrayObjectAdapter mRowsAdapter; private BackgroundHelper mBackgroundHelper = new BackgroundHelper(); + // For good performance, it's important to use a single instance of + // a card presenter for all rows using that presenter. + final static CardPresenter sCardPresenter = new CardPresenter(); + @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); @@ -77,17 +82,18 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm } }); if (TEST_ENTRANCE_TRANSITION) { - // don't run entrance transition if Activity is restored. + // don't run entrance transition if fragment is restored. if (savedInstanceState == null) { prepareEntranceTransition(); } - // simulate delay loading data - new Handler().postDelayed(new Runnable() { - public void run() { - startEntranceTransition(); - } - }, 2000); } + // simulates in a real world use case data being loaded two seconds later + new Handler().postDelayed(new Runnable() { + public void run() { + loadData(); + startEntranceTransition(); + } + }, 2000); } private void setupRows() { @@ -100,25 +106,24 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm mRowsAdapter = new ArrayObjectAdapter(lrp); - // For good performance, it's important to use a single instance of - // a card presenter for all rows using that presenter. - final CardPresenter cardPresenter = new CardPresenter(); + setAdapter(mRowsAdapter); + } + private void loadData() { for (int i = 0; i < NUM_ROWS; ++i) { - ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter); + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter); listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1)); listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_2)); listRowAdapter.add(new PhotoItem("Android TV", "by Google", R.drawable.gallery_photo_3)); listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4)); listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5)); - listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6)); + listRowAdapter.add(new PhotoItem("This is a test", "Open GuidedStepFragment", R.drawable.gallery_photo_6)); listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7)); listRowAdapter.add(new PhotoItem("Leanback", "open BrowseActivity", R.drawable.gallery_photo_8)); HeaderItem header = new HeaderItem(i, "Row " + i); mRowsAdapter.add(new ListRow(header, listRowAdapter)); } - setAdapter(mRowsAdapter); } private final class ItemViewClickedListener implements OnItemViewClickedListener { @@ -128,7 +133,12 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm Intent intent; Bundle bundle; - if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) { + if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) { + GuidedStepFragment.add(getFragmentManager(), + new GuidedStepActivity.FirstStepFragment(), + android.R.id.content); + return; + } else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) { intent = new Intent(getActivity(), BrowseActivity.class); bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) .toBundle(); diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java index ac0b67154..b03c14c28 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java @@ -49,7 +49,7 @@ public class DetailsPresenterSelectionActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - GuidedStepFragment.add(getFragmentManager(), new SetupFragment()); + GuidedStepFragment.addAsRoot(this, new SetupFragment(), android.R.id.content); } private static void addAction(List<GuidedAction> actions, long id, String title, String desc) { diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java index 1c1928283..f189a05c5 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java @@ -58,12 +58,7 @@ public class GuidedStepActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "onCreate"); super.onCreate(savedInstanceState); - GuidedStepFragment.add(getFragmentManager(), new FirstStepFragment()); - getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { - @Override public void onGlobalLayout() { - //Log.v(TAG, "onGlobalLayout", new Exception()); - } - }); + GuidedStepFragment.addAsRoot(this, new FirstStepFragment(), android.R.id.content); } @Override @@ -139,14 +134,14 @@ public class GuidedStepActivity extends Activity { public void onGuidedActionClicked(GuidedAction action) { FragmentManager fm = getFragmentManager(); if (action.getId() == CONTINUE) { - GuidedStepFragment.add(fm, new SecondStepFragment()); + GuidedStepFragment.add(fm, new SecondStepFragment(), android.R.id.content); } else { getActivity().finish(); } } } - private static class SecondStepFragment extends GuidedStepFragment { + public static class SecondStepFragment extends GuidedStepFragment { @Override public Guidance onCreateGuidance(Bundle savedInstanceState) { @@ -173,7 +168,7 @@ public class GuidedStepActivity extends Activity { } - private static class ThirdStepFragment extends GuidedStepFragment { + public static class ThirdStepFragment extends GuidedStepFragment { private int mSelectedOption = DEFAULT_OPTION; @@ -221,7 +216,11 @@ public class GuidedStepActivity extends Activity { public void onGuidedActionClicked(GuidedAction action) { if (action.getId() == CONTINUE) { FragmentManager fm = getFragmentManager(); - GuidedStepFragment.add(fm, new FourthStepFragment(mSelectedOption)); + FourthStepFragment f = new FourthStepFragment(); + Bundle arguments = new Bundle(); + arguments.putInt(FourthStepFragment.EXTRA_OPTION, mSelectedOption); + f.setArguments(arguments); + GuidedStepFragment.add(fm, f, android.R.id.content); } else { mSelectedOption = getSelectedActionPosition()-1; } @@ -229,18 +228,23 @@ public class GuidedStepActivity extends Activity { } - private static class FourthStepFragment extends GuidedStepFragment { - private final int mOption; + public static class FourthStepFragment extends GuidedStepFragment { + public static final String EXTRA_OPTION = "extra_option"; + + public FourthStepFragment() { + } - public FourthStepFragment(int option) { - mOption = option; + public int getOption() { + Bundle b = getArguments(); + if (b == null) return 0; + return b.getInt(EXTRA_OPTION, 0); } @Override public Guidance onCreateGuidance(Bundle savedInstanceState) { String title = getString(R.string.guidedstep_fourth_title); String breadcrumb = getString(R.string.guidedstep_fourth_breadcrumb); - String description = "You chose: " + OPTION_NAMES[mOption]; + String description = "You chose: " + OPTION_NAMES[getOption()]; Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon); return new Guidance(title, description, breadcrumb, icon); } diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java index c1b60f41c..57723e2ad 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java @@ -20,6 +20,7 @@ import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.support.v4.app.ActivityOptionsCompat; import android.support.v17.leanback.app.GuidedStepFragment; import android.support.v17.leanback.widget.GuidedAction; import android.support.v17.leanback.widget.GuidanceStylist.Guidance; @@ -39,7 +40,8 @@ public class MainActivity extends Activity { super.onCreate(savedInstanceState); mGuidedStepFragment = new StepFragment(); - GuidedStepFragment.add(getFragmentManager(), mGuidedStepFragment); + GuidedStepFragment.addAsRoot(this, mGuidedStepFragment, android.R.id.content); + } public static class StepFragment extends GuidedStepFragment { @@ -90,7 +92,9 @@ public class MainActivity extends Activity { public void onGuidedActionClicked(GuidedAction action) { Intent intent = action.getIntent(); if (intent != null) { - startActivity(intent); + Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()) + .toBundle(); + startActivity(intent, bundle); } } diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java index 9ffaf66f5..fe664dd7a 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java @@ -15,6 +15,7 @@ package com.example.android.leanback; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; import android.support.v17.leanback.widget.ArrayObjectAdapter; import android.support.v17.leanback.widget.Presenter; import android.support.v17.leanback.widget.RowPresenter; @@ -34,6 +35,7 @@ public class VerticalGridFragment extends android.support.v17.leanback.app.Verti private static final int NUM_COLUMNS = 3; private static final int NUM_ITEMS = 50; private static final int HEIGHT = 200; + private static final boolean TEST_ENTRANCE_TRANSITION = true; private static class Adapter extends ArrayObjectAdapter { public Adapter(StringPresenter presenter) { @@ -54,6 +56,25 @@ public class VerticalGridFragment extends android.support.v17.leanback.app.Verti setTitle("Leanback Vertical Grid Demo"); setupFragment(); + if (TEST_ENTRANCE_TRANSITION) { + // don't run entrance transition if fragment is restored. + if (savedInstanceState == null) { + prepareEntranceTransition(); + } + } + // simulates in a real world use case data being loaded two seconds later + new Handler().postDelayed(new Runnable() { + public void run() { + loadData(); + startEntranceTransition(); + } + }, 2000); + } + + private void loadData() { + for (int i = 0; i < NUM_ITEMS; i++) { + mAdapter.add(Integer.toString(i)); + } } private void setupFragment() { @@ -62,9 +83,6 @@ public class VerticalGridFragment extends android.support.v17.leanback.app.Verti setGridPresenter(gridPresenter); mAdapter = new Adapter(new StringPresenter()); - for (int i = 0; i < NUM_ITEMS; i++) { - mAdapter.add(Integer.toString(i)); - } setAdapter(mAdapter); setOnItemViewSelectedListener(new OnItemViewSelectedListener() { |