1a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn/* 2a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * Copyright (C) 2014 The Android Open Source Project 3a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * 4a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * in compliance with the License. You may obtain a copy of the License at 6a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * 7a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * http://www.apache.org/licenses/LICENSE-2.0 8a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * 9a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * Unless required by applicable law or agreed to in writing, software distributed under the License 10a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * or implied. See the License for the specific language governing permissions and limitations under 12a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * the License. 13a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn */ 14a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournpackage android.support.v17.leanback.widget; 15a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 164121f22713bbed467a977ec0d867ef53989ff374Dake Guimport android.app.Activity; 17d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stoutimport android.content.Context; 18e23ea69ec54eb70d17239ba671fcd75563780553Craig Stoutimport android.graphics.Bitmap; 19d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stoutimport android.graphics.Color; 207728d53c5c50e8ed807e8f4a189e34684de04800Craig Stoutimport android.graphics.drawable.Drawable; 21e23ea69ec54eb70d17239ba671fcd75563780553Craig Stoutimport android.graphics.drawable.BitmapDrawable; 2209d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stoutimport android.graphics.drawable.ColorDrawable; 23d825ff3727407a154a35e20b3a9adc79e57879b9Dake Guimport android.os.Handler; 2470acb0c19be3831a2080e4f902324de16bfbf62eTor Norbyeimport android.support.annotation.ColorInt; 25a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.support.v17.leanback.R; 262e0c922430f8c285b4325da52d69c09451069c93Craig Stoutimport android.support.v7.widget.RecyclerView; 272e0c922430f8c285b4325da52d69c09451069c93Craig Stoutimport android.util.Log; 28d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stoutimport android.util.TypedValue; 2960bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stoutimport android.view.KeyEvent; 30a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.view.LayoutInflater; 31a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.view.View; 32a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.view.ViewGroup; 33a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.widget.FrameLayout; 34a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport android.widget.ImageView; 35a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 36a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournimport java.util.Collection; 37a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 38a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn/** 39a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Renders a {@link DetailsOverviewRow} to display an overview of an item. 40a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Typically this row will be the first row in a fragment 4180d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * such as the {@link android.support.v17.leanback.app.DetailsFragment 42a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * DetailsFragment}. The View created by the DetailsOverviewRowPresenter is made in three parts: 4318c8048f17940359b8bce99cb46d24337bff8997Dake Gu * ImageView on the left, action list view on the bottom and a customizable detailed 4418c8048f17940359b8bce99cb46d24337bff8997Dake Gu * description view on the right. 45a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn * 4618c8048f17940359b8bce99cb46d24337bff8997Dake Gu * <p>The detailed description is rendered using a {@link Presenter} passed in 47a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * {@link #DetailsOverviewRowPresenter(Presenter)}. Typically this will be an instance of 48a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * {@link AbstractDetailsDescriptionPresenter}. The application can access the 49a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * detailed description ViewHolder from {@link ViewHolder#mDetailsDescriptionViewHolder}. 5018c8048f17940359b8bce99cb46d24337bff8997Dake Gu * </p> 5118c8048f17940359b8bce99cb46d24337bff8997Dake Gu * 5218c8048f17940359b8bce99cb46d24337bff8997Dake Gu * <p> 5318c8048f17940359b8bce99cb46d24337bff8997Dake Gu * To participate in activity transition, call {@link #setSharedElementEnterTransition(Activity, 5418c8048f17940359b8bce99cb46d24337bff8997Dake Gu * String)} during Activity's onCreate(). 5518c8048f17940359b8bce99cb46d24337bff8997Dake Gu * </p> 5618c8048f17940359b8bce99cb46d24337bff8997Dake Gu * 5718c8048f17940359b8bce99cb46d24337bff8997Dake Gu * <p> 5818c8048f17940359b8bce99cb46d24337bff8997Dake Gu * Because transition support and layout are fully controlled by DetailsOverviewRowPresenter, 5918c8048f17940359b8bce99cb46d24337bff8997Dake Gu * developer can not override DetailsOverviewRowPresenter.ViewHolder for adding/replacing views 60a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * of DetailsOverviewRowPresenter. If further customization is required beyond replacing 61a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * the detailed description, the application should create a new row presenter class. 6218c8048f17940359b8bce99cb46d24337bff8997Dake Gu * </p> 630246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * @deprecated Use {@link FullWidthDetailsOverviewRowPresenter} 64a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn */ 650246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu@Deprecated 66a83005b70853ea52c5d98910762344de16b850a8Tim Kilbournpublic class DetailsOverviewRowPresenter extends RowPresenter { 67a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 68a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn private static final String TAG = "DetailsOverviewRowPresenter"; 69a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn private static final boolean DEBUG = false; 70a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 712e0c922430f8c285b4325da52d69c09451069c93Craig Stout private static final int MORE_ACTIONS_FADE_MS = 100; 72d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu private static final long DEFAULT_TIMEOUT = 5000; 732e0c922430f8c285b4325da52d69c09451069c93Craig Stout 7471fddded48048acfa744ac352166770c91a1c2b1Dake Gu class ActionsItemBridgeAdapter extends ItemBridgeAdapter { 7571fddded48048acfa744ac352166770c91a1c2b1Dake Gu DetailsOverviewRowPresenter.ViewHolder mViewHolder; 7671fddded48048acfa744ac352166770c91a1c2b1Dake Gu 7771fddded48048acfa744ac352166770c91a1c2b1Dake Gu ActionsItemBridgeAdapter(DetailsOverviewRowPresenter.ViewHolder viewHolder) { 7871fddded48048acfa744ac352166770c91a1c2b1Dake Gu mViewHolder = viewHolder; 7971fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 8071fddded48048acfa744ac352166770c91a1c2b1Dake Gu 8171fddded48048acfa744ac352166770c91a1c2b1Dake Gu @Override 8271fddded48048acfa744ac352166770c91a1c2b1Dake Gu public void onBind(final ItemBridgeAdapter.ViewHolder ibvh) { 833a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout if (mViewHolder.getOnItemViewClickedListener() != null || 843a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout mActionClickedListener != null) { 8571fddded48048acfa744ac352166770c91a1c2b1Dake Gu ibvh.getPresenter().setOnClickListener( 8671fddded48048acfa744ac352166770c91a1c2b1Dake Gu ibvh.getViewHolder(), new View.OnClickListener() { 8771fddded48048acfa744ac352166770c91a1c2b1Dake Gu @Override 8871fddded48048acfa744ac352166770c91a1c2b1Dake Gu public void onClick(View v) { 893a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout if (mViewHolder.getOnItemViewClickedListener() != null) { 903a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout mViewHolder.getOnItemViewClickedListener().onItemClicked( 9171fddded48048acfa744ac352166770c91a1c2b1Dake Gu ibvh.getViewHolder(), ibvh.getItem(), 9271fddded48048acfa744ac352166770c91a1c2b1Dake Gu mViewHolder, mViewHolder.getRow()); 9371fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 9471fddded48048acfa744ac352166770c91a1c2b1Dake Gu if (mActionClickedListener != null) { 9571fddded48048acfa744ac352166770c91a1c2b1Dake Gu mActionClickedListener.onActionClicked((Action) ibvh.getItem()); 9671fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 9771fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 9871fddded48048acfa744ac352166770c91a1c2b1Dake Gu }); 9971fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 10071fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 10171fddded48048acfa744ac352166770c91a1c2b1Dake Gu @Override 10271fddded48048acfa744ac352166770c91a1c2b1Dake Gu public void onUnbind(final ItemBridgeAdapter.ViewHolder ibvh) { 1033a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout if (mViewHolder.getOnItemViewClickedListener() != null || 1043a5038bd0fefa5628a31a21422bf2126c53472a9Craig Stout mActionClickedListener != null) { 10571fddded48048acfa744ac352166770c91a1c2b1Dake Gu ibvh.getPresenter().setOnClickListener(ibvh.getViewHolder(), null); 10671fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 10771fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 10871fddded48048acfa744ac352166770c91a1c2b1Dake Gu @Override 10971fddded48048acfa744ac352166770c91a1c2b1Dake Gu public void onAttachedToWindow(ItemBridgeAdapter.ViewHolder viewHolder) { 11071fddded48048acfa744ac352166770c91a1c2b1Dake Gu // Remove first to ensure we don't add ourselves more than once. 11171fddded48048acfa744ac352166770c91a1c2b1Dake Gu viewHolder.itemView.removeOnLayoutChangeListener(mViewHolder.mLayoutChangeListener); 11271fddded48048acfa744ac352166770c91a1c2b1Dake Gu viewHolder.itemView.addOnLayoutChangeListener(mViewHolder.mLayoutChangeListener); 11371fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 11471fddded48048acfa744ac352166770c91a1c2b1Dake Gu @Override 11571fddded48048acfa744ac352166770c91a1c2b1Dake Gu public void onDetachedFromWindow(ItemBridgeAdapter.ViewHolder viewHolder) { 11671fddded48048acfa744ac352166770c91a1c2b1Dake Gu viewHolder.itemView.removeOnLayoutChangeListener(mViewHolder.mLayoutChangeListener); 11771fddded48048acfa744ac352166770c91a1c2b1Dake Gu mViewHolder.checkFirstAndLastPosition(false); 11871fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 11971fddded48048acfa744ac352166770c91a1c2b1Dake Gu } 12071fddded48048acfa744ac352166770c91a1c2b1Dake Gu 12180d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn /** 12280d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * A ViewHolder for the DetailsOverviewRow. 12380d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn */ 124cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout public final class ViewHolder extends RowPresenter.ViewHolder { 12509d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout final FrameLayout mOverviewFrame; 126b34a2372153298ebdc3e148e1c1f3b3924efab08Dake Gu final ViewGroup mOverviewView; 127a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn final ImageView mImageView; 1284121f22713bbed467a977ec0d867ef53989ff374Dake Gu final ViewGroup mRightPanel; 129a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn final FrameLayout mDetailsDescriptionFrame; 130a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn final HorizontalGridView mActionsRow; 13118c8048f17940359b8bce99cb46d24337bff8997Dake Gu public final Presenter.ViewHolder mDetailsDescriptionViewHolder; 13283118b72d02074ee35f07ad0253579565c16882bCraig Stout int mNumItems; 13383118b72d02074ee35f07ad0253579565c16882bCraig Stout boolean mShowMoreRight; 13483118b72d02074ee35f07ad0253579565c16882bCraig Stout boolean mShowMoreLeft; 13571fddded48048acfa744ac352166770c91a1c2b1Dake Gu ItemBridgeAdapter mActionBridgeAdapter; 136d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu final Handler mHandler = new Handler(); 137a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 138d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu final Runnable mUpdateDrawableCallback = new Runnable() { 139d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 140d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu public void run() { 141d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu bindImageDrawable(ViewHolder.this); 142d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 143d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu }; 144d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 145d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu final DetailsOverviewRow.Listener mListener = new DetailsOverviewRow.Listener() { 146d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 147d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu public void onImageDrawableChanged(DetailsOverviewRow row) { 148d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu mHandler.removeCallbacks(mUpdateDrawableCallback); 149d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu mHandler.post(mUpdateDrawableCallback); 150d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 151d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 152d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 153d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu public void onItemChanged(DetailsOverviewRow row) { 154d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu if (mDetailsDescriptionViewHolder != null) { 155d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu mDetailsPresenter.onUnbindViewHolder(mDetailsDescriptionViewHolder); 156d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 157d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu mDetailsPresenter.onBindViewHolder(mDetailsDescriptionViewHolder, row.getItem()); 158d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 159d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 160d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 161d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu public void onActionsAdapterChanged(DetailsOverviewRow row) { 162d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu bindActions(row.getActionsAdapter()); 163d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 164d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu }; 165d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 166d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu void bindActions(ObjectAdapter adapter) { 167cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionBridgeAdapter.setAdapter(adapter); 16837eb0e53ba5363bbe15314c7d19d4b2d6024a5aaDake Gu mActionsRow.setAdapter(mActionBridgeAdapter); 169cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mNumItems = mActionBridgeAdapter.getItemCount(); 1702e0c922430f8c285b4325da52d69c09451069c93Craig Stout 17183118b72d02074ee35f07ad0253579565c16882bCraig Stout mShowMoreRight = false; 17283118b72d02074ee35f07ad0253579565c16882bCraig Stout mShowMoreLeft = true; 17383118b72d02074ee35f07ad0253579565c16882bCraig Stout showMoreLeft(false); 17483118b72d02074ee35f07ad0253579565c16882bCraig Stout } 17583118b72d02074ee35f07ad0253579565c16882bCraig Stout 176819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout final View.OnLayoutChangeListener mLayoutChangeListener = 177819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout new View.OnLayoutChangeListener() { 178819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout 179819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout @Override 180819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout public void onLayoutChange(View v, int left, int top, int right, 181819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 182819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout if (DEBUG) Log.v(TAG, "onLayoutChange " + v); 183819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout checkFirstAndLastPosition(false); 184819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout } 185819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout }; 186819b4e55bcf37847548a55c5dac0dfa8323975f6Craig Stout 187cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout final OnChildSelectedListener mChildSelectedListener = new OnChildSelectedListener() { 188cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout @Override 189cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout public void onChildSelected(ViewGroup parent, View view, int position, long id) { 190cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout dispatchItemSelection(view); 191cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 192cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout }; 193cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout 194cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout void dispatchItemSelection(View view) { 195cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout if (!isSelected()) { 196cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout return; 197cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 198cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout ItemBridgeAdapter.ViewHolder ibvh = (ItemBridgeAdapter.ViewHolder) (view != null ? 199cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionsRow.getChildViewHolder(view) : 200cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionsRow.findViewHolderForPosition(mActionsRow.getSelectedPosition())); 201cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout if (ibvh == null) { 202cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout if (getOnItemViewSelectedListener() != null) { 203cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout getOnItemViewSelectedListener().onItemSelected(null, null, 204cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout ViewHolder.this, getRow()); 205cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 206cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } else { 207cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout if (getOnItemViewSelectedListener() != null) { 208cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout getOnItemViewSelectedListener().onItemSelected(ibvh.getViewHolder(), ibvh.getItem(), 209cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout ViewHolder.this, getRow()); 210cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 211cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 212cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout }; 213cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout 21483118b72d02074ee35f07ad0253579565c16882bCraig Stout final RecyclerView.OnScrollListener mScrollListener = 21583118b72d02074ee35f07ad0253579565c16882bCraig Stout new RecyclerView.OnScrollListener() { 2162e0c922430f8c285b4325da52d69c09451069c93Craig Stout 2172e0c922430f8c285b4325da52d69c09451069c93Craig Stout @Override 218cef7b4942bacc862ea4eac66952e9f7aba027d18Yigit Boyar public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 2192e0c922430f8c285b4325da52d69c09451069c93Craig Stout } 2202e0c922430f8c285b4325da52d69c09451069c93Craig Stout @Override 221cef7b4942bacc862ea4eac66952e9f7aba027d18Yigit Boyar public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 22283118b72d02074ee35f07ad0253579565c16882bCraig Stout checkFirstAndLastPosition(true); 2232e0c922430f8c285b4325da52d69c09451069c93Craig Stout } 22483118b72d02074ee35f07ad0253579565c16882bCraig Stout }; 22583118b72d02074ee35f07ad0253579565c16882bCraig Stout 22683118b72d02074ee35f07ad0253579565c16882bCraig Stout private int getViewCenter(View view) { 22783118b72d02074ee35f07ad0253579565c16882bCraig Stout return (view.getRight() - view.getLeft()) / 2; 22883118b72d02074ee35f07ad0253579565c16882bCraig Stout } 22983118b72d02074ee35f07ad0253579565c16882bCraig Stout 23083118b72d02074ee35f07ad0253579565c16882bCraig Stout private void checkFirstAndLastPosition(boolean fromScroll) { 23183118b72d02074ee35f07ad0253579565c16882bCraig Stout RecyclerView.ViewHolder viewHolder; 2322e0c922430f8c285b4325da52d69c09451069c93Craig Stout 23383118b72d02074ee35f07ad0253579565c16882bCraig Stout viewHolder = mActionsRow.findViewHolderForPosition(mNumItems - 1); 23483118b72d02074ee35f07ad0253579565c16882bCraig Stout boolean showRight = (viewHolder == null || 23583118b72d02074ee35f07ad0253579565c16882bCraig Stout viewHolder.itemView.getRight() > mActionsRow.getWidth()); 23683118b72d02074ee35f07ad0253579565c16882bCraig Stout 23783118b72d02074ee35f07ad0253579565c16882bCraig Stout viewHolder = mActionsRow.findViewHolderForPosition(0); 23883118b72d02074ee35f07ad0253579565c16882bCraig Stout boolean showLeft = (viewHolder == null || viewHolder.itemView.getLeft() < 0); 23983118b72d02074ee35f07ad0253579565c16882bCraig Stout 24083118b72d02074ee35f07ad0253579565c16882bCraig Stout if (DEBUG) Log.v(TAG, "checkFirstAndLast fromScroll " + fromScroll + 24183118b72d02074ee35f07ad0253579565c16882bCraig Stout " showRight " + showRight + " showLeft " + showLeft); 24283118b72d02074ee35f07ad0253579565c16882bCraig Stout 24383118b72d02074ee35f07ad0253579565c16882bCraig Stout showMoreRight(showRight); 24483118b72d02074ee35f07ad0253579565c16882bCraig Stout showMoreLeft(showLeft); 24583118b72d02074ee35f07ad0253579565c16882bCraig Stout } 24683118b72d02074ee35f07ad0253579565c16882bCraig Stout 24783118b72d02074ee35f07ad0253579565c16882bCraig Stout private void showMoreLeft(boolean show) { 24883118b72d02074ee35f07ad0253579565c16882bCraig Stout if (show != mShowMoreLeft) { 24983118b72d02074ee35f07ad0253579565c16882bCraig Stout mActionsRow.setFadingLeftEdge(show); 25083118b72d02074ee35f07ad0253579565c16882bCraig Stout mShowMoreLeft = show; 2512e0c922430f8c285b4325da52d69c09451069c93Craig Stout } 25283118b72d02074ee35f07ad0253579565c16882bCraig Stout } 2532e0c922430f8c285b4325da52d69c09451069c93Craig Stout 25483118b72d02074ee35f07ad0253579565c16882bCraig Stout private void showMoreRight(boolean show) { 25583118b72d02074ee35f07ad0253579565c16882bCraig Stout if (show != mShowMoreRight) { 25683118b72d02074ee35f07ad0253579565c16882bCraig Stout mActionsRow.setFadingRightEdge(show); 25783118b72d02074ee35f07ad0253579565c16882bCraig Stout mShowMoreRight = show; 2582e0c922430f8c285b4325da52d69c09451069c93Craig Stout } 2592e0c922430f8c285b4325da52d69c09451069c93Craig Stout } 2602e0c922430f8c285b4325da52d69c09451069c93Craig Stout 26180d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn /** 26280d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Constructor for the ViewHolder. 26380d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * 26480d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * @param rootView The root View that this view holder will be attached 26580d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * to. 26680d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn */ 26718c8048f17940359b8bce99cb46d24337bff8997Dake Gu public ViewHolder(View rootView, Presenter detailsPresenter) { 268a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn super(rootView); 26909d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout mOverviewFrame = (FrameLayout) rootView.findViewById(R.id.details_frame); 270b34a2372153298ebdc3e148e1c1f3b3924efab08Dake Gu mOverviewView = (ViewGroup) rootView.findViewById(R.id.details_overview); 271a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn mImageView = (ImageView) rootView.findViewById(R.id.details_overview_image); 2724121f22713bbed467a977ec0d867ef53989ff374Dake Gu mRightPanel = (ViewGroup) rootView.findViewById(R.id.details_overview_right_panel); 273a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn mDetailsDescriptionFrame = 2744121f22713bbed467a977ec0d867ef53989ff374Dake Gu (FrameLayout) mRightPanel.findViewById(R.id.details_overview_description); 27570c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn mActionsRow = 2764121f22713bbed467a977ec0d867ef53989ff374Dake Gu (HorizontalGridView) mRightPanel.findViewById(R.id.details_overview_actions); 277ceb7ab2ddd6e157cd4ade0f14a382c39428163c4Dake Gu mActionsRow.setHasOverlappingRendering(false); 2782e0c922430f8c285b4325da52d69c09451069c93Craig Stout mActionsRow.setOnScrollListener(mScrollListener); 279cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionsRow.setAdapter(mActionBridgeAdapter); 280cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionsRow.setOnChildSelectedListener(mChildSelectedListener); 2812e0c922430f8c285b4325da52d69c09451069c93Craig Stout 2822e0c922430f8c285b4325da52d69c09451069c93Craig Stout final int fadeLength = rootView.getResources().getDimensionPixelSize( 2832e0c922430f8c285b4325da52d69c09451069c93Craig Stout R.dimen.lb_details_overview_actions_fade_size); 2842e0c922430f8c285b4325da52d69c09451069c93Craig Stout mActionsRow.setFadingRightEdgeLength(fadeLength); 2852e0c922430f8c285b4325da52d69c09451069c93Craig Stout mActionsRow.setFadingLeftEdgeLength(fadeLength); 28618c8048f17940359b8bce99cb46d24337bff8997Dake Gu mDetailsDescriptionViewHolder = 28718c8048f17940359b8bce99cb46d24337bff8997Dake Gu detailsPresenter.onCreateViewHolder(mDetailsDescriptionFrame); 28818c8048f17940359b8bce99cb46d24337bff8997Dake Gu mDetailsDescriptionFrame.addView(mDetailsDescriptionViewHolder.view); 289a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 290a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 291a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 292a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn private final Presenter mDetailsPresenter; 293cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout private OnActionClickedListener mActionClickedListener; 294cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout 295d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout private int mBackgroundColor = Color.TRANSPARENT; 296d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout private boolean mBackgroundColorSet; 297d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout private boolean mIsStyleLarge = true; 298a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 2994121f22713bbed467a977ec0d867ef53989ff374Dake Gu private DetailsOverviewSharedElementHelper mSharedElementHelper; 3004121f22713bbed467a977ec0d867ef53989ff374Dake Gu 301a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn /** 30280d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Constructor for a DetailsOverviewRowPresenter. 30380d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * 30480d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * @param detailsPresenter The {@link Presenter} used to render the detailed 30580d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * description of the row. 306a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn */ 307a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn public DetailsOverviewRowPresenter(Presenter detailsPresenter) { 308d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout setHeaderPresenter(null); 309a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn setSelectEffectEnabled(false); 310a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn mDetailsPresenter = detailsPresenter; 311a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 312a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 31370c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn /** 31480d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Sets the listener for Action click events. 31570c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn */ 31670c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn public void setOnActionClickedListener(OnActionClickedListener listener) { 317cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout mActionClickedListener = listener; 31870c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn } 31970c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn 32070c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn /** 321a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Returns the listener for Action click events. 32270c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn */ 32370c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn public OnActionClickedListener getOnActionClickedListener() { 324cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout return mActionClickedListener; 32570c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn } 32670c858a5ca5b7ed9862e2edfa43912faecf42f96Tim Kilbourn 327d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout /** 32880d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Sets the background color. If not set, a default from the theme will be used. 329d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout */ 33070acb0c19be3831a2080e4f902324de16bfbf62eTor Norbye public void setBackgroundColor(@ColorInt int color) { 331d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout mBackgroundColor = color; 332d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout mBackgroundColorSet = true; 333d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 334d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 335d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout /** 33680d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Returns the background color. If no background color was set, transparent 33780d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * is returned. 338d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout */ 33970acb0c19be3831a2080e4f902324de16bfbf62eTor Norbye @ColorInt 340d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout public int getBackgroundColor() { 341d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout return mBackgroundColor; 342d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 343d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 344d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout /** 34580d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * Sets the layout style to be large or small. This affects the height of 34680d04d2265fe28800fcbc7e8cc7d6d229a7913d8Tim Kilbourn * the overview, including the text description. The default is large. 347d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout */ 348d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout public void setStyleLarge(boolean large) { 349d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout mIsStyleLarge = large; 350d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 351d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 352d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout /** 353d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout * Returns true if the layout style is large. 354d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout */ 355d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout public boolean isStyleLarge() { 356d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout return mIsStyleLarge; 357d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 358d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 3599de363b8db05106b03d115c266859fe200d41db7Dake Gu /** 360a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Sets the enter transition of target activity to be 361d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * transiting into overview row created by this presenter. The transition will 362a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * be cancelled if the overview image is not loaded in the timeout period. 3634121f22713bbed467a977ec0d867ef53989ff374Dake Gu * <p> 3644121f22713bbed467a977ec0d867ef53989ff374Dake Gu * It assumes shared element passed from calling activity is an ImageView; 365a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * the shared element transits to overview image on the starting edge of the detail 3664121f22713bbed467a977ec0d867ef53989ff374Dake Gu * overview row, while bounds of overview row grows and reveals text 367a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * and action buttons. 3684121f22713bbed467a977ec0d867ef53989ff374Dake Gu * <p> 3694121f22713bbed467a977ec0d867ef53989ff374Dake Gu * The method must be invoked in target Activity's onCreate(). 3709de363b8db05106b03d115c266859fe200d41db7Dake Gu */ 3714121f22713bbed467a977ec0d867ef53989ff374Dake Gu public final void setSharedElementEnterTransition(Activity activity, 372d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu String sharedElementName, long timeoutMs) { 3734121f22713bbed467a977ec0d867ef53989ff374Dake Gu if (mSharedElementHelper == null) { 3744121f22713bbed467a977ec0d867ef53989ff374Dake Gu mSharedElementHelper = new DetailsOverviewSharedElementHelper(); 3754121f22713bbed467a977ec0d867ef53989ff374Dake Gu } 376d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu mSharedElementHelper.setSharedElementEnterTransition(activity, sharedElementName, 377d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu timeoutMs); 378d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu } 379d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu 380d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu /** 381a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Sets the enter transition of target activity to be 382d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * transiting into overview row created by this presenter. The transition will 383d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * be cancelled if overview image is not loaded in a default timeout period. 384d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * <p> 385d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * It assumes shared element passed from calling activity is an ImageView; 386a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * the shared element transits to overview image on the starting edge of the detail 387d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * overview row, while bounds of overview row grows and reveals text 388a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * and action buttons. 389d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * <p> 390d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu * The method must be invoked in target Activity's onCreate(). 391d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu */ 392d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu public final void setSharedElementEnterTransition(Activity activity, 393d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu String sharedElementName) { 394d391b19d1bf663ce300b0f4550e6fbaa7e12b0d4Dake Gu setSharedElementEnterTransition(activity, sharedElementName, DEFAULT_TIMEOUT); 3959de363b8db05106b03d115c266859fe200d41db7Dake Gu } 3969de363b8db05106b03d115c266859fe200d41db7Dake Gu 397d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout private int getDefaultBackgroundColor(Context context) { 398d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout TypedValue outValue = new TypedValue(); 39946443cb5b092f1d9156342645088eead9da026f6Dake Gu if (context.getTheme().resolveAttribute(R.attr.defaultBrandColor, outValue, true)) { 40046443cb5b092f1d9156342645088eead9da026f6Dake Gu return context.getResources().getColor(outValue.resourceId); 40146443cb5b092f1d9156342645088eead9da026f6Dake Gu } 40246443cb5b092f1d9156342645088eead9da026f6Dake Gu return context.getResources().getColor(R.color.lb_default_brand_color); 403d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 404d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 405d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 406cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout protected void onRowViewSelected(RowPresenter.ViewHolder vh, boolean selected) { 407cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout super.onRowViewSelected(vh, selected); 408cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout if (selected) { 409cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout ((ViewHolder) vh).dispatchItemSelection(null); 410cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 411cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout } 412cf992de2d34abb8228dc6cb39fffe97346823a37Craig Stout 413a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn @Override 414a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) { 415a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn View v = LayoutInflater.from(parent.getContext()) 416a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn .inflate(R.layout.lb_details_overview, parent, false); 41718c8048f17940359b8bce99cb46d24337bff8997Dake Gu ViewHolder vh = new ViewHolder(v, mDetailsPresenter); 418a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 41950ea3557b75826b3f51480715df1973472e345c4Craig Stout initDetailsOverview(vh); 420d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 421a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn return vh; 422a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 423a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 424e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout private int getCardHeight(Context context) { 425d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout int resId = mIsStyleLarge ? R.dimen.lb_details_overview_height_large : 426d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout R.dimen.lb_details_overview_height_small; 427e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout return context.getResources().getDimensionPixelSize(resId); 428e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 429d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 43060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout private void initDetailsOverview(final ViewHolder vh) { 43171fddded48048acfa744ac352166770c91a1c2b1Dake Gu vh.mActionBridgeAdapter = new ActionsItemBridgeAdapter(vh); 43226c145aadca16cba3f294cc93c36edc6e9c98e6aCraig Stout final View overview = vh.mOverviewFrame; 43350ea3557b75826b3f51480715df1973472e345c4Craig Stout ViewGroup.LayoutParams lp = overview.getLayoutParams(); 434e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout lp.height = getCardHeight(overview.getContext()); 43550ea3557b75826b3f51480715df1973472e345c4Craig Stout overview.setLayoutParams(lp); 436d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 43709d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout if (!getSelectEffectEnabled()) { 43809d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout vh.mOverviewFrame.setForeground(null); 43909d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout } 44060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout vh.mActionsRow.setOnUnhandledKeyListener(new BaseGridView.OnUnhandledKeyListener() { 44160bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout @Override 44260bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout public boolean onUnhandledKey(KeyEvent event) { 44360bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout if (vh.getOnKeyListener() != null) { 44460bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout if (vh.getOnKeyListener().onKey(vh.view, event.getKeyCode(), event)) { 44560bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout return true; 44660bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout } 44760bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout } 44860bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout return false; 44960bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout } 45060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout }); 451d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout } 452d2c235afaa4119599fd8bfc9bff65c5af9a8fdc1Craig Stout 4537728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout private static int getNonNegativeWidth(Drawable drawable) { 4547728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout final int width = (drawable == null) ? 0 : drawable.getIntrinsicWidth(); 4557728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout return (width > 0 ? width : 0); 4567728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 4577728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout 4587728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout private static int getNonNegativeHeight(Drawable drawable) { 4597728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout final int height = (drawable == null) ? 0 : drawable.getIntrinsicHeight(); 4607728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout return (height > 0 ? height : 0); 4617728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 4627728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout 463d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu private void bindImageDrawable(ViewHolder vh) { 464d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu DetailsOverviewRow row = (DetailsOverviewRow) vh.getRow(); 465e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout 466e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout ViewGroup.MarginLayoutParams layoutParams = 467e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout (ViewGroup.MarginLayoutParams) vh.mImageView.getLayoutParams(); 468e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout final int cardHeight = getCardHeight(vh.mImageView.getContext()); 469e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout final int verticalMargin = vh.mImageView.getResources().getDimensionPixelSize( 470e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout R.dimen.lb_details_overview_image_margin_vertical); 471e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout final int horizontalMargin = vh.mImageView.getResources().getDimensionPixelSize( 472e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout R.dimen.lb_details_overview_image_margin_horizontal); 4737728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout final int drawableWidth = getNonNegativeWidth(row.getImageDrawable()); 4747728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout final int drawableHeight = getNonNegativeHeight(row.getImageDrawable()); 4757728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout 476e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout boolean scaleImage = row.isImageScaleUpAllowed(); 477e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout boolean useMargin = false; 478e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout 4797728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (row.getImageDrawable() != null) { 4807728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout boolean landscape = false; 4817728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout 4827728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout // If large style and landscape image we always use margin. 4837728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (drawableWidth > drawableHeight) { 4847728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout landscape = true; 4857728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (mIsStyleLarge) { 4867728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout useMargin = true; 4877728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 488e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 4897728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout // If long dimension bigger than the card height we scale down. 4907728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if ((landscape && drawableWidth > cardHeight) || 4917728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout (!landscape && drawableHeight > cardHeight)) { 492e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout scaleImage = true; 493e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 4947728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout // If we're not scaling to fit the card height then we always use margin. 4957728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (!scaleImage) { 4967728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout useMargin = true; 4977728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 4987728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout // If using margin than may need to scale down. 4997728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (useMargin && !scaleImage) { 5007728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout if (landscape && drawableWidth > cardHeight - horizontalMargin) { 5017728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout scaleImage = true; 5027728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } else if (!landscape && drawableHeight > cardHeight - 2 * verticalMargin) { 5037728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout scaleImage = true; 5047728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 5057728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout } 506e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 507e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout 5084f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout final int bgColor = mBackgroundColorSet ? mBackgroundColor : 5094f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout getDefaultBackgroundColor(vh.mOverviewView.getContext()); 5104f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout 511e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout if (useMargin) { 5124e602840ed6741b44a536826799625ad0be4b3c6Kris Giesing layoutParams.setMarginStart(horizontalMargin); 513e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout layoutParams.topMargin = layoutParams.bottomMargin = verticalMargin; 5146b447e693090017258eb48a51ae4119fb0f5119eCraig Stout vh.mOverviewFrame.setBackgroundColor(bgColor); 51572a2146f4c3e6dbb84a5f9f92e7ab42d142dab04Dake Gu vh.mRightPanel.setBackground(null); 516836443f4a3b69eb7a88ffeb65a39e11439dee55eDake Gu vh.mImageView.setBackground(null); 517e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } else { 518e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout layoutParams.leftMargin = layoutParams.topMargin = layoutParams.bottomMargin = 0; 5194f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout vh.mRightPanel.setBackgroundColor(bgColor); 5204f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout vh.mImageView.setBackgroundColor(bgColor); 5216b447e693090017258eb48a51ae4119fb0f5119eCraig Stout vh.mOverviewFrame.setBackground(null); 522e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 5236b447e693090017258eb48a51ae4119fb0f5119eCraig Stout RoundedRectHelper.getInstance().setClipToRoundedOutline(vh.mOverviewFrame, true); 5246b447e693090017258eb48a51ae4119fb0f5119eCraig Stout 525e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout if (scaleImage) { 526e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setScaleType(ImageView.ScaleType.FIT_START); 527e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setAdjustViewBounds(true); 528e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setMaxWidth(cardHeight); 529e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; 530e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; 531e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } else { 532e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setScaleType(ImageView.ScaleType.CENTER); 533e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setAdjustViewBounds(false); 534e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; 535e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout // Limit width to the card height 5367728d53c5c50e8ed807e8f4a189e34684de04800Craig Stout layoutParams.width = Math.min(cardHeight, drawableWidth); 537e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout } 538e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setLayoutParams(layoutParams); 539e23ea69ec54eb70d17239ba671fcd75563780553Craig Stout vh.mImageView.setImageDrawable(row.getImageDrawable()); 5404121f22713bbed467a977ec0d867ef53989ff374Dake Gu if (row.getImageDrawable() != null && mSharedElementHelper != null) { 5414121f22713bbed467a977ec0d867ef53989ff374Dake Gu mSharedElementHelper.onBindToDrawable(vh); 5424121f22713bbed467a977ec0d867ef53989ff374Dake Gu } 543a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 544a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn 545a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn @Override 546d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu protected void onBindRowViewHolder(RowPresenter.ViewHolder holder, Object item) { 547d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu super.onBindRowViewHolder(holder, item); 548d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 549d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu DetailsOverviewRow row = (DetailsOverviewRow) item; 550d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu ViewHolder vh = (ViewHolder) holder; 551d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 552d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu bindImageDrawable(vh); 553d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu mDetailsPresenter.onBindViewHolder(vh.mDetailsDescriptionViewHolder, row.getItem()); 554d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu vh.bindActions(row.getActionsAdapter()); 555d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu row.addListener(vh.mListener); 556d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu } 557d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu 558d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu @Override 559cb13a318e577e14461eb008071dddf762847de42Dake Gu protected void onUnbindRowViewHolder(RowPresenter.ViewHolder holder) { 560a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn ViewHolder vh = (ViewHolder) holder; 561d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu DetailsOverviewRow dor = (DetailsOverviewRow) vh.getRow(); 562d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu dor.removeListener(vh.mListener); 563a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn if (vh.mDetailsDescriptionViewHolder != null) { 564a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn mDetailsPresenter.onUnbindViewHolder(vh.mDetailsDescriptionViewHolder); 565a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 566fc8f0f06c93c224d86b85c8b9f7ae737968fd797Dake Gu super.onUnbindRowViewHolder(holder); 567a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn } 56809d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout 56909d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout @Override 57009d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout public final boolean isUsingDefaultSelectEffect() { 57109d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout return false; 57209d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout } 57309d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout 57409d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout @Override 57509d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout protected void onSelectLevelChanged(RowPresenter.ViewHolder holder) { 57609d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout super.onSelectLevelChanged(holder); 57709d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout if (getSelectEffectEnabled()) { 57809d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout ViewHolder vh = (ViewHolder) holder; 57909d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout int dimmedColor = vh.mColorDimmer.getPaint().getColor(); 58009d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout ((ColorDrawable) vh.mOverviewFrame.getForeground().mutate()).setColor(dimmedColor); 58109d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout } 58209d52d76912e3689cc2b58b7bb3f44b923915fd8Craig Stout } 58326d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout 58426d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout @Override 58526d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout protected void onRowViewAttachedToWindow(RowPresenter.ViewHolder vh) { 58626d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout super.onRowViewAttachedToWindow(vh); 58726d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout if (mDetailsPresenter != null) { 58826d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout mDetailsPresenter.onViewAttachedToWindow( 58926d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout ((ViewHolder) vh).mDetailsDescriptionViewHolder); 59026d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout } 59126d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout } 59226d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout 59326d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout @Override 59426d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout protected void onRowViewDetachedFromWindow(RowPresenter.ViewHolder vh) { 59526d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout super.onRowViewDetachedFromWindow(vh); 59626d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout if (mDetailsPresenter != null) { 59726d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout mDetailsPresenter.onViewDetachedFromWindow( 59826d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout ((ViewHolder) vh).mDetailsDescriptionViewHolder); 59926d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout } 60026d11212c4a66b075807888caacac5ee7fc10dc1Craig Stout } 601a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn} 602