RowContainerView.java revision 3bcad88cbf4488e747d84893c35f2351b8f84afe
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.annotation.ColorInt;
21import android.support.v17.leanback.R;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.LinearLayout;
27
28/**
29 * RowContainerView wraps header and user defined row view
30 */
31final class RowContainerView extends LinearLayout {
32
33    private ViewGroup mHeaderDock;
34    private Drawable mForeground;
35    private boolean mForegroundBoundsChanged = true;
36
37    public RowContainerView(Context context) {
38        this(context, null, 0);
39    }
40
41    public RowContainerView(Context context, AttributeSet attrs) {
42        this(context, attrs, 0);
43    }
44
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
55    public void addHeaderView(View headerView) {
56        if (mHeaderDock.indexOfChild(headerView) < 0) {
57            mHeaderDock.addView(headerView, 0);
58        }
59    }
60
61    public void removeHeaderView(View headerView) {
62        if (mHeaderDock.indexOfChild(headerView) >= 0) {
63            mHeaderDock.removeView(headerView);
64        }
65    }
66
67    public void addRowView(View view) {
68        addView(view);
69    }
70
71    public void showHeader(boolean show) {
72        mHeaderDock.setVisibility(show ? View.VISIBLE : View.GONE);
73    }
74
75    @Override
76    public void setForeground(Drawable d) {
77        mForeground = d;
78        setWillNotDraw(mForeground == null);
79        invalidate();
80    }
81
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
92    public Drawable getForeground() {
93        return mForeground;
94    }
95
96    @Override
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
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
115    public boolean hasOverlappingRendering() {
116        return false;
117    }
118}
119