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