forked from hitherejoe/Vineyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconHeaderItemPresenter.java
83 lines (70 loc) · 3.04 KB
/
IconHeaderItemPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.hitherejoe.vineyard.ui.presenter;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.RowHeaderPresenter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.hitherejoe.vineyard.R;
public class IconHeaderItemPresenter extends RowHeaderPresenter {
private float mUnselectedAlpha;
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup) {
mUnselectedAlpha = viewGroup.getResources()
.getFraction(R.fraction.lb_browse_header_unselect_alpha, 1, 1);
LayoutInflater inflater = (LayoutInflater) viewGroup.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_icon_header, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object o) {
HeaderItem headerItem = ((ListRow) o).getHeaderItem();
View rootView = viewHolder.view;
rootView.setAlpha(mUnselectedAlpha);
setIconDrawable(headerItem.getName(), rootView);
TextView label = (TextView) rootView.findViewById(R.id.header_label);
label.setText(headerItem.getName());
}
public void setIconDrawable(String name, View rootView) {
Resources res = rootView.getResources();
String[] categories = res.getStringArray(R.array.categories);
TypedArray resources = res.obtainTypedArray(R.array.icons);
int drawableResource = 0;
if (name.equals(res.getString(R.string.header_text_options))) {
drawableResource = R.drawable.ic_settings_white_24dp;
} else {
for (int i = 0; i < categories.length; i++) {
if (categories[i].equals(name)) {
drawableResource = resources.getResourceId(i, 0);
break;
}
}
}
resources.recycle();
if (drawableResource != 0) {
ImageView iconView = (ImageView) rootView.findViewById(R.id.header_icon);
Drawable icon = res.getDrawable(drawableResource, null);
iconView.setImageDrawable(icon);
}
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
// no op
}
// TODO: TEMP - remove me when leanback onCreateViewHolder no longer sets the mUnselectAlpha,AND
// also assumes the xml inflation will return a RowHeaderView
@Override
protected void onSelectLevelChanged(RowHeaderPresenter.ViewHolder holder) {
// this is a temporary fix
holder.view.setAlpha(mUnselectedAlpha + holder.getSelectLevel() *
(1.0f - mUnselectedAlpha));
}
}