ControlBar.java revision 6e28bd2903a19cd4560d94413161bd3fa36542e6
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.content.Context; 17import android.graphics.Rect; 18import android.util.AttributeSet; 19import android.view.View; 20import android.view.ViewGroup; 21import android.widget.LinearLayout; 22 23import java.util.ArrayList; 24 25class ControlBar extends LinearLayout { 26 27 public interface OnChildFocusedListener { 28 public void onChildFocusedListener(View child, View focused); 29 } 30 31 private int mChildMarginFromCenter; 32 private OnChildFocusedListener mOnChildFocusedListener; 33 int mLastFocusIndex = -1; 34 35 public ControlBar(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 } 38 39 public ControlBar(Context context, AttributeSet attrs, int defStyle) { 40 super(context, attrs, defStyle); 41 } 42 43 @Override 44 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 45 if (getChildCount() > 0) { 46 int index = mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount() 47 ? mLastFocusIndex : getChildCount() / 2; 48 if (getChildAt(index).requestFocus(direction, previouslyFocusedRect)) { 49 return true; 50 } 51 } 52 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 53 } 54 55 @Override 56 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { 57 if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { 58 if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { 59 views.add(getChildAt(mLastFocusIndex)); 60 } else if (getChildCount() > 0) { 61 views.add(getChildAt(getChildCount() / 2)); 62 } 63 } else { 64 super.addFocusables(views, direction, focusableMode); 65 } 66 } 67 68 public void setOnChildFocusedListener(OnChildFocusedListener listener) { 69 mOnChildFocusedListener = listener; 70 } 71 72 public void setChildMarginFromCenter(int marginFromCenter) { 73 mChildMarginFromCenter = marginFromCenter; 74 } 75 76 @Override 77 public void requestChildFocus (View child, View focused) { 78 super.requestChildFocus(child, focused); 79 mLastFocusIndex = indexOfChild(child); 80 if (mOnChildFocusedListener != null) { 81 mOnChildFocusedListener.onChildFocusedListener(child, focused); 82 } 83 } 84 85 @Override 86 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 87 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 88 if (mChildMarginFromCenter <= 0) { 89 return; 90 } 91 92 int totalExtraMargin = 0; 93 for (int i = 0; i < getChildCount() - 1; i++) { 94 View first = getChildAt(i); 95 View second = getChildAt(i+1); 96 int measuredWidth = first.getMeasuredWidth() + second.getMeasuredWidth(); 97 int marginStart = mChildMarginFromCenter - measuredWidth / 2; 98 LayoutParams lp = (LayoutParams) second.getLayoutParams(); 99 int extraMargin = marginStart - lp.getMarginStart(); 100 lp.setMarginStart(marginStart); 101 second.setLayoutParams(lp); 102 totalExtraMargin += extraMargin; 103 } 104 setMeasuredDimension(getMeasuredWidth() + totalExtraMargin, getMeasuredHeight()); 105 } 106} 107