Merge "SupportLeanbackDemo: demo of custom theme of ImageCardView" into mnc-ub-dev
This commit is contained in:
@@ -29,4 +29,22 @@
|
||||
<style name="Widget.Example.Leanback.SecondStepGuidanceIconStyle" parent="Widget.Leanback.GuidanceIconStyle">
|
||||
<item name="android:padding">24dp</item>
|
||||
</style>
|
||||
|
||||
<style name="MyImageCardViewStyle" parent="Widget.Leanback.ImageCardViewStyle">
|
||||
<item name="lbImageCardViewType">Title|Content|IconOnRight</item>
|
||||
</style>
|
||||
|
||||
<style name="MyImageCardViewTitleStyleText" parent="TextAppearance.Leanback.ImageCardView.Title">
|
||||
<item name="android:textColor">@android:color/holo_blue_bright</item>
|
||||
</style>
|
||||
|
||||
<style name="MyImageCardViewTitleStyle" parent="Widget.Leanback.ImageCardView.TitleStyle">
|
||||
<item name="android:textAppearance">@style/MyImageCardViewTitleStyleText</item>
|
||||
</style>
|
||||
|
||||
<style name="MyImageCardViewTheme" parent="Theme.Leanback">
|
||||
<item name="imageCardViewStyle">@style/MyImageCardViewStyle</item>
|
||||
<item name="imageCardViewTitleStyle">@style/MyImageCardViewTitleStyle</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -37,15 +37,14 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
|
||||
|
||||
private static final boolean TEST_ENTRANCE_TRANSITION = true;
|
||||
private static final int NUM_ROWS = 10;
|
||||
// Row heights default to wrap content
|
||||
private static final boolean USE_FIXED_ROW_HEIGHT = false;
|
||||
|
||||
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();
|
||||
final CardPresenter mCardPresenter = new CardPresenter();
|
||||
final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme);
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -99,11 +98,6 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
|
||||
private void setupRows() {
|
||||
ListRowPresenter lrp = new ListRowPresenter();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
mRowsAdapter = new ArrayObjectAdapter(lrp);
|
||||
|
||||
setAdapter(mRowsAdapter);
|
||||
@@ -111,7 +105,8 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm
|
||||
|
||||
private void loadData() {
|
||||
for (int i = 0; i < NUM_ROWS; ++i) {
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter);
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ?
|
||||
mCardPresenter : mCardPresenter2);
|
||||
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));
|
||||
|
||||
@@ -39,15 +39,14 @@ public class BrowseSupportFragment extends android.support.v17.leanback.app.Brow
|
||||
|
||||
private static final boolean TEST_ENTRANCE_TRANSITION = true;
|
||||
private static final int NUM_ROWS = 10;
|
||||
// Row heights default to wrap content
|
||||
private static final boolean USE_FIXED_ROW_HEIGHT = false;
|
||||
|
||||
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();
|
||||
final CardPresenter mCardPresenter = new CardPresenter();
|
||||
final CardPresenter mCardPresenter2 = new CardPresenter(R.style.MyImageCardViewTheme);
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -101,11 +100,6 @@ public class BrowseSupportFragment extends android.support.v17.leanback.app.Brow
|
||||
private void setupRows() {
|
||||
ListRowPresenter lrp = new ListRowPresenter();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
mRowsAdapter = new ArrayObjectAdapter(lrp);
|
||||
|
||||
setAdapter(mRowsAdapter);
|
||||
@@ -113,7 +107,8 @@ public class BrowseSupportFragment extends android.support.v17.leanback.app.Brow
|
||||
|
||||
private void loadData() {
|
||||
for (int i = 0; i < NUM_ROWS; ++i) {
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter);
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter((i & 1) == 0 ?
|
||||
mCardPresenter : mCardPresenter2);
|
||||
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));
|
||||
|
||||
@@ -15,11 +15,13 @@ package com.example.android.leanback;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import com.example.android.leanback.R;
|
||||
import android.support.v17.leanback.widget.ImageCardView;
|
||||
import android.support.v17.leanback.widget.Presenter;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.View.MeasureSpec;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.widget.TextView;
|
||||
@@ -32,38 +34,56 @@ public class CardPresenter extends Presenter {
|
||||
private static final int IMAGE_HEIGHT_DP = 120;
|
||||
|
||||
private static Random sRand = new Random();
|
||||
private static int sRowHeight = 0;
|
||||
private static int sExpandedRowHeight = 0;
|
||||
private int mRowHeight = 0;
|
||||
private int mExpandedRowHeight = 0;
|
||||
|
||||
private static void setupRowHeights(Context context) {
|
||||
if (sRowHeight == 0) {
|
||||
private int mCardThemeResId;
|
||||
private Context mContextThemeWrapper;
|
||||
|
||||
public CardPresenter(int cardThemeResId) {
|
||||
mCardThemeResId = cardThemeResId;
|
||||
}
|
||||
|
||||
public CardPresenter() {
|
||||
mCardThemeResId = 0;
|
||||
}
|
||||
|
||||
private void setupRowHeights(Context context) {
|
||||
if (mRowHeight == 0) {
|
||||
float density = context.getResources().getDisplayMetrics().density;
|
||||
int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
|
||||
|
||||
ImageCardView v = new ImageCardView(context);
|
||||
v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
|
||||
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
|
||||
sRowHeight = v.getMeasuredHeight();
|
||||
mRowHeight = v.getMeasuredHeight();
|
||||
v.setActivated(true);
|
||||
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
|
||||
sExpandedRowHeight = v.getMeasuredHeight();
|
||||
mExpandedRowHeight = v.getMeasuredHeight();
|
||||
}
|
||||
}
|
||||
|
||||
public static int getRowHeight(Context context) {
|
||||
public int getRowHeight(Context context) {
|
||||
setupRowHeights(context);
|
||||
return sRowHeight;
|
||||
return mRowHeight;
|
||||
}
|
||||
|
||||
public static int getExpandedRowHeight(Context context) {
|
||||
public int getExpandedRowHeight(Context context) {
|
||||
setupRowHeights(context);
|
||||
return sExpandedRowHeight;
|
||||
return mExpandedRowHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent) {
|
||||
Log.d(TAG, "onCreateViewHolder");
|
||||
ImageCardView v = new ImageCardView(parent.getContext());
|
||||
Context context = parent.getContext();
|
||||
if (mCardThemeResId != 0) {
|
||||
if (mContextThemeWrapper == null) {
|
||||
mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId);
|
||||
}
|
||||
context = mContextThemeWrapper;
|
||||
}
|
||||
ImageCardView v = new ImageCardView(context);
|
||||
v.setFocusable(true);
|
||||
v.setFocusableInTouchMode(true);
|
||||
// Randomly makes image view crop as a square or just stretch to original
|
||||
|
||||
@@ -73,17 +73,17 @@ public class RowsFragment extends android.support.v17.leanback.app.RowsFragment
|
||||
private void setupRows() {
|
||||
ListRowPresenter lrp = new ListRowPresenter();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(cardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(cardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
mRowsAdapter = new ArrayObjectAdapter(lrp);
|
||||
|
||||
for (int i = 0; i < NUM_ROWS; ++i) {
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
|
||||
listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
|
||||
|
||||
@@ -75,17 +75,17 @@ public class RowsSupportFragment extends android.support.v17.leanback.app.RowsSu
|
||||
private void setupRows() {
|
||||
ListRowPresenter lrp = new ListRowPresenter();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (USE_FIXED_ROW_HEIGHT) {
|
||||
lrp.setRowHeight(cardPresenter.getRowHeight(getActivity()));
|
||||
lrp.setExpandedRowHeight(cardPresenter.getExpandedRowHeight(getActivity()));
|
||||
}
|
||||
|
||||
mRowsAdapter = new ArrayObjectAdapter(lrp);
|
||||
|
||||
for (int i = 0; i < NUM_ROWS; ++i) {
|
||||
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
|
||||
listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
|
||||
|
||||
Reference in New Issue
Block a user