HorizontalHoverCardSwitcher.java revision 8b068ddbbf22a246eab49ec25a2f7c3abfbdca51
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import android.graphics.Rect; 17import android.view.View; 18import android.view.ViewGroup; 19import android.view.View.MeasureSpec; 20import android.view.ViewGroup.MarginLayoutParams; 21 22/** 23 * Helper class that stay bellow a HorizontalGridView and shows a hover card and align 24 * the hover card left to left of selected child view. If there is no space when scroll 25 * to the end, right edge hover card will be aligned to right of parent view excluding 26 * right padding. 27 */ 28public final class HorizontalHoverCardSwitcher extends PresenterSwitcher { 29 // left and right of selected card view 30 int mCardLeft, mCardRight; 31 32 private int[] mTmpOffsets = new int[2]; 33 private Rect mTmpRect = new Rect(); 34 35 @Override 36 protected void insertView(View view) { 37 // append hovercard to the end of container 38 getParentViewGroup().addView(view); 39 } 40 41 @Override 42 protected void onViewSelected(View view) { 43 int rightLimit = getParentViewGroup().getWidth() - 44 getParentViewGroup().getPaddingRight(); 45 // measure the hover card width, if it's too large, align hover card 46 // right edge with row view's right edge 47 view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 48 MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); 49 if (mCardLeft + view.getMeasuredWidth() > rightLimit) { 50 params.leftMargin = rightLimit - view.getMeasuredWidth(); 51 } else { 52 params.leftMargin = mCardLeft; 53 } 54 view.requestLayout(); 55 } 56 57 /** 58 * Select a childView inside a grid view and create/bind a corresponding hover card view 59 * for the object. 60 */ 61 public void select(BaseListView gridView, View childView, Object object) { 62 ViewGroup parent = getParentViewGroup(); 63 gridView.getViewSelectedOffsets(childView, mTmpOffsets); 64 mTmpRect.set(0, 0, childView.getWidth(), childView.getHeight()); 65 parent.offsetDescendantRectToMyCoords(childView, mTmpRect); 66 mCardLeft = mTmpRect.left - mTmpOffsets[0]; 67 mCardRight = mTmpRect.right - mTmpOffsets[0]; 68 select(object); 69 } 70 71}