我的GridLayout
填充了FrameLayout
。 FrameLayout
由ImageView
和TextView
组成:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/activity_home_grid_column_width"
android:layout_height="@dimen/activity_home_grid_column_height"
android:layout_gravity="center"
android:focusable="true">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:duplicateParentState="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
</FrameLayout>
在代码中,我将OnFocusChangeListener
附加到frameLayout实例。使用充气器获取此实例。
...
final FrameLayout frameLayout = (FrameLayout) getActivity().getLayoutInflater()
.inflate(R.layout.home_grid_item1, homeGrid, false);
final ImageView image = (ImageView) frameLayout.findViewById(R.id.image);
final TextView title = (TextView) frameLayout.findViewById(R.id.title);
...
处理程序包含一个逻辑:
if (hasFocus) {
if (selectedUrl != null) {
picasso.load(selectedUrl).into(image);
} else {
frameLayout.requestFocus(true);
}
//frameLayout.requestFocus();
//title.requestFocus();
title.setSelected(true);
} else {
picasso.load(normalUrl).into(image);
//frameLayout.clearFocus();
frameLayout.setSelected(false);
//title.clearFocus();
title.setSelected(false);
}
在我使用d-pad选择项目时更改图像时,背景颜色不会根据StateListDrawable
设置为背景而改变:
final StateListDrawable rootBgColorList = new StateListDrawable();
rootBgColorList.addState(new int[]{}, new ColorDrawable(Color.parseColor(homeConfiguration.getGridItemNormalBgColor())));
rootBgColorList.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_focused}, new ColorDrawable(Color.parseColor(homeConfiguration.getGridItemSelectedBgColor())));
frameLayout.setBackground(rootBgColorList);
final StateListDrawable titleBgColorList = new StateListDrawable();
titleBgColorList.addState(new int[]{}, new ColorDrawable(Color.parseColor(homeConfiguration.getGridItemTextNormalBgColor())));
titleBgColorList.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_focused}, new ColorDrawable(Color.parseColor(homeConfiguration.getGridItemTextSelectedBgColor())));
title.setBackground(titleBgColorList);
title.setText(item.getTitle());
title.setTextColor(Color.parseColor(homeConfiguration.getGridItemTextColor()));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, homeConfiguration.getGridItemTextSize());
当我选择frameLayout根视图时,无法弄清楚如何激活背景选择器。?