1/* 2 * Copyright (C) 2016 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.Color; 18import android.util.TypedValue; 19import android.view.ContextThemeWrapper; 20import android.view.LayoutInflater; 21import android.view.View; 22import android.support.v17.leanback.R; 23import android.view.ViewGroup; 24import android.widget.TextView; 25 26/** 27 * Abstract presenter class for rendering the header for a list of media items in a playlist. 28 * The presenter creates a {@link ViewHolder} for the TextView holding the header text. 29 * <p> 30 * Subclasses of this class must override {@link 31 * #onBindMediaListHeaderViewHolder(ViewHolder, Object)} in order to bind their header text to 32 * the media list header view. 33 * </p> 34 * <p> 35 * {@link AbstractMediaItemPresenter} can be used in conjunction with this presenter in order to 36 * display a playlist with a header view. 37 * </p> 38 */ 39public abstract class AbstractMediaListHeaderPresenter extends RowPresenter{ 40 41 private final Context mContext; 42 private int mBackgroundColor = Color.TRANSPARENT; 43 private boolean mBackgroundColorSet; 44 45 /** 46 * The ViewHolder for the {@link AbstractMediaListHeaderPresenter}. It references the TextView 47 * that places the header text provided by the data binder. 48 */ 49 public static class ViewHolder extends RowPresenter.ViewHolder { 50 51 private final TextView mHeaderView; 52 53 public ViewHolder(View view) { 54 super(view); 55 mHeaderView = (TextView) view.findViewById(R.id.mediaListHeader); 56 } 57 58 /** 59 * 60 * @return the header {@link TextView} responsible for rendering the playlist header text. 61 */ 62 public TextView getHeaderView() { 63 return mHeaderView; 64 } 65 } 66 67 /** 68 * Constructor used for creating an abstract media-list header presenter of a given theme. 69 * @param context The context the user of this presenter is running in. 70 * @param mThemeResId The resource id of the desired theme used for styling of this presenter. 71 */ 72 public AbstractMediaListHeaderPresenter(Context context, int mThemeResId) { 73 mContext = new ContextThemeWrapper(context.getApplicationContext(), mThemeResId); 74 setHeaderPresenter(null); 75 } 76 77 /** 78 * Constructor used for creating an abstract media-list header presenter. 79 * The styling for this presenter is extracted from Context of parent in 80 * {@link #createRowViewHolder(ViewGroup)}. 81 */ 82 public AbstractMediaListHeaderPresenter() { 83 mContext = null; 84 setHeaderPresenter(null); 85 } 86 87 @Override 88 public boolean isUsingDefaultSelectEffect() { 89 return false; 90 } 91 92 @Override 93 protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) { 94 Context context = (mContext != null) ? mContext : parent.getContext(); 95 View view = LayoutInflater.from(context).inflate(R.layout.lb_media_list_header, 96 parent, false); 97 view.setFocusable(false); 98 view.setFocusableInTouchMode(false); 99 ViewHolder vh = new ViewHolder(view); 100 if (mBackgroundColorSet) { 101 vh.view.setBackgroundColor(mBackgroundColor); 102 } 103 return vh; 104 } 105 106 @Override 107 protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) { 108 super.onBindRowViewHolder(vh, item); 109 onBindMediaListHeaderViewHolder((ViewHolder) vh, item); 110 } 111 112 /** 113 * Sets the background color for the row views within the playlist. 114 * If this is not set, a default color, defaultBrandColor, from theme is used. 115 * This defaultBrandColor defaults to android:attr/colorPrimary on v21, if it's specified. 116 * @param color The ARGB color used to set as the header text background color. 117 */ 118 public void setBackgroundColor(int color) { 119 mBackgroundColorSet = true; 120 mBackgroundColor = color; 121 } 122 123 /** 124 * Binds the playlist header data model provided by the user to the {@link ViewHolder} 125 * provided by the {@link AbstractMediaListHeaderPresenter}. 126 * The subclasses of this presenter can access and bind the text view corresponding to the 127 * header by calling {@link ViewHolder#getHeaderView()}, on the 128 * {@link ViewHolder} provided as the argument {@code vh} by this presenter. 129 * 130 * @param vh The ViewHolder for this {@link AbstractMediaListHeaderPresenter}. 131 * @param item The header data object being presented. 132 */ 133 protected abstract void onBindMediaListHeaderViewHolder(ViewHolder vh, Object item); 134 135} 136