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.Canvas; 18import android.graphics.drawable.ColorDrawable; 19import android.graphics.drawable.Drawable; 20import android.support.v17.leanback.R; 21import android.util.AttributeSet; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.LinearLayout; 26 27/** 28 * RowContainerView wraps header and user defined row view 29 */ 30final class RowContainerView extends LinearLayout { 31 32 private ViewGroup mHeaderDock; 33 private Drawable mForeground; 34 private boolean mForegroundBoundsChanged = true; 35 36 public RowContainerView(Context context) { 37 this(context, null, 0); 38 } 39 40 public RowContainerView(Context context, AttributeSet attrs) { 41 this(context, attrs, 0); 42 } 43 44 public RowContainerView(Context context, AttributeSet attrs, int defStyle) { 45 super(context, attrs, defStyle); 46 setOrientation(VERTICAL); 47 LayoutInflater inflater = LayoutInflater.from(context); 48 inflater.inflate(R.layout.lb_row_container, this); 49 50 mHeaderDock = (ViewGroup) findViewById(R.id.lb_row_container_header_dock); 51 setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 52 } 53 54 public void addHeaderView(View headerView) { 55 if (mHeaderDock.indexOfChild(headerView) < 0) { 56 mHeaderDock.addView(headerView, 0); 57 } 58 } 59 60 public void removeHeaderView(View headerView) { 61 if (mHeaderDock.indexOfChild(headerView) >= 0) { 62 mHeaderDock.removeView(headerView); 63 } 64 } 65 66 public void addRowView(View view) { 67 addView(view); 68 } 69 70 public void showHeader(boolean show) { 71 mHeaderDock.setVisibility(show ? View.VISIBLE : View.GONE); 72 } 73 74 public void setForeground(Drawable d) { 75 mForeground = d; 76 setWillNotDraw(mForeground == null); 77 invalidate(); 78 } 79 80 public void setForegroundColor(int color) { 81 if (mForeground instanceof ColorDrawable) { 82 ((ColorDrawable) mForeground.mutate()).setColor(color); 83 invalidate(); 84 } else { 85 setForeground(new ColorDrawable(color)); 86 } 87 } 88 89 public Drawable getForeground() { 90 return mForeground; 91 } 92 93 @Override 94 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 95 super.onSizeChanged(w, h, oldw, oldh); 96 mForegroundBoundsChanged = true; 97 } 98 99 @Override 100 public void draw(Canvas canvas) { 101 super.draw(canvas); 102 if (mForeground != null) { 103 if (mForegroundBoundsChanged) { 104 mForegroundBoundsChanged = false; 105 mForeground.setBounds(0, 0, getWidth(), getHeight()); 106 } 107 mForeground.draw(canvas); 108 } 109 } 110} 111