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 com.android.tv.quicksettings; 15 16 import android.content.Intent; 17 import android.support.v7.widget.RecyclerView; 18 import android.view.LayoutInflater; 19 import android.view.View; 20 import android.view.ViewGroup; 21 import android.widget.TextView; 22 23 import java.util.ArrayList; 24 25 public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.ViewHolder> { 26 27 private static final float FOCUSED_SCALE = 1f; 28 private static final float UNFOCUSED_SCALE = 0.5f; 29 30 private final ArrayList<Setting> mSettings; 31 private final int mPivotX; 32 private final int mPivotY; 33 private final SettingClickedListener mSettingClickedListener; 34 DialogAdapter(ArrayList<Setting> settings, int pivotX, int pivotY, SettingClickedListener settingClickedListener)35 public DialogAdapter(ArrayList<Setting> settings, int pivotX, int pivotY, 36 SettingClickedListener settingClickedListener) { 37 mSettings = settings; 38 mPivotX = pivotX; 39 mPivotY = pivotY; 40 mSettingClickedListener = settingClickedListener; 41 } 42 43 @Override onCreateViewHolder(ViewGroup parent, int viewType)44 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 45 View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_setting, parent, 46 false); 47 final ViewHolder vh = new ViewHolder(v); 48 v.setPivotX(mPivotX); 49 v.setPivotY(mPivotY); 50 v.setScaleX(UNFOCUSED_SCALE); 51 v.setScaleY(UNFOCUSED_SCALE); 52 v.setOnClickListener(new View.OnClickListener() { 53 @Override 54 public void onClick(View v) { 55 Setting s = vh.mSetting; 56 if (s != null) { 57 mSettingClickedListener.onSettingClicked(s); 58 } 59 } 60 }); 61 v.setOnFocusChangeListener(new View.OnFocusChangeListener() { 62 @Override 63 public void onFocusChange(View v, boolean focusGained) { 64 float scale = focusGained ? FOCUSED_SCALE : UNFOCUSED_SCALE; 65 v.animate().cancel(); 66 v.animate().scaleX(scale).scaleY(scale).setDuration( 67 v.getContext().getResources() 68 .getInteger(android.R.integer.config_shortAnimTime)) 69 .start(); 70 } 71 }); 72 return vh; 73 } 74 75 @Override onBindViewHolder(ViewHolder holder, int position)76 public void onBindViewHolder(ViewHolder holder, int position) { 77 Setting s = mSettings.get(position); 78 holder.setSetting(s); 79 holder.mTitle.setText(s.getTitle()); 80 } 81 82 @Override onViewRecycled(ViewHolder holder)83 public void onViewRecycled(ViewHolder holder) { 84 holder.setSetting(null); 85 } 86 87 @Override getItemCount()88 public int getItemCount() { 89 return mSettings.size(); 90 } 91 92 public static class ViewHolder extends RecyclerView.ViewHolder { 93 private final TextView mTitle; 94 private Setting mSetting; 95 ViewHolder(View itemView)96 public ViewHolder(View itemView) { 97 super(itemView); 98 mTitle = (TextView) itemView.findViewById(R.id.setting_title); 99 } 100 setSetting(Setting setting)101 public void setSetting(Setting setting) { 102 mSetting = setting; 103 } 104 } 105 } 106