ActionPresenterSelector.java revision 8e3566285de4ac771d6188f62fe947e23d371a3d
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.support.v17.leanback.R; 17import android.text.TextUtils; 18import android.view.LayoutInflater; 19import android.view.View; 20import android.view.ViewGroup; 21import android.widget.Button; 22 23class ActionPresenterSelector extends PresenterSelector { 24 25 private final Presenter mOneLineActionPresenter = new OneLineActionPresenter(); 26 private final Presenter mTwoLineActionPresenter = new TwoLineActionPresenter(); 27 28 @Override 29 public Presenter getPresenter(Object item) { 30 Action action = (Action) item; 31 if (TextUtils.isEmpty(action.getLabel2())) { 32 return mOneLineActionPresenter; 33 } else { 34 return mTwoLineActionPresenter; 35 } 36 } 37 38 static class ActionViewHolder extends Presenter.ViewHolder { 39 Action mAction; 40 Button mButton; 41 42 public ActionViewHolder(View view) { 43 super(view); 44 mButton = (Button) view.findViewById(R.id.lb_action_button); 45 } 46 } 47 48 class OneLineActionPresenter extends Presenter { 49 @Override 50 public ViewHolder onCreateViewHolder(ViewGroup parent) { 51 View v = LayoutInflater.from(parent.getContext()) 52 .inflate(R.layout.lb_action_1_line, parent, false); 53 return new ActionViewHolder(v); 54 } 55 56 @Override 57 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 58 Action action = (Action) item; 59 ActionViewHolder vh = (ActionViewHolder) viewHolder; 60 vh.mAction = action; 61 vh.mButton.setText(action.getLabel1()); 62 } 63 64 @Override 65 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 66 ((ActionViewHolder) viewHolder).mAction = null; 67 } 68 } 69 70 class TwoLineActionPresenter extends Presenter { 71 @Override 72 public ViewHolder onCreateViewHolder(ViewGroup parent) { 73 View v = LayoutInflater.from(parent.getContext()) 74 .inflate(R.layout.lb_action_2_lines, parent, false); 75 return new ActionViewHolder(v); 76 } 77 78 @Override 79 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 80 Action action = (Action) item; 81 ActionViewHolder vh = (ActionViewHolder) viewHolder; 82 vh.mAction = action; 83 84 if (action.getIcon() != null) { 85 final int startPadding = vh.view.getResources() 86 .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_start); 87 final int endPadding = vh.view.getResources() 88 .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_end); 89 vh.view.setPaddingRelative(startPadding, 0, endPadding, 0); 90 } else { 91 final int padding = vh.view.getResources() 92 .getDimensionPixelSize(R.dimen.lb_action_padding_horizontal); 93 vh.view.setPaddingRelative(padding, 0, padding, 0); 94 } 95 vh.mButton.setCompoundDrawablesWithIntrinsicBounds(action.getIcon(), null, null, null); 96 97 CharSequence line1 = action.getLabel1(); 98 CharSequence line2 = action.getLabel2(); 99 if (TextUtils.isEmpty(line1)) { 100 vh.mButton.setText(line2); 101 } else if (TextUtils.isEmpty(line2)) { 102 vh.mButton.setText(line1); 103 } else { 104 vh.mButton.setText(line1 + "\n" + line2); 105 } 106 } 107 108 @Override 109 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 110 ActionViewHolder vh = (ActionViewHolder) viewHolder; 111 vh.mButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); 112 vh.view.setPadding(0, 0, 0, 0); 113 vh.mAction = null; 114 } 115 } 116} 117