我在HorizontalScrollView和ImageButton(也是ImageView)中存在一个问题。 当HorizontalScrollView被填充时,我已经动态地分配了一个drawable选择器。Android ImageView:动态选择器在HorizontalScrollView中不起作用
一切都好,因为您可以点击并触发OnClickListener。问题出在物品状态。点击ImageButton(被触摸屏幕)时,显示正确的可绘图,但是当我摘下手指时(未触摸屏幕),显示默认图像而不是按下的图像。 如果我分配静态可绘制选择器(xml选择器),则会发生同样的问题。
这是动态选择的代码:
公共静态StateListDrawable setStateListDrawable(上下文语境,TypedArray图像){
StateListDrawable states = new StateListDrawable();
try{
states.addState(new int[] {android.R.attr.state_pressed},images.getDrawable(1));
states.addState(new int[] {android.R.attr.state_focused},images.getDrawable(1));
states.addState(new int[] { },images.getDrawable(0));
}catch(Exception e){
int id = context.getResources().getIdentifier("ID1", "array", context.getPackageName());
images = context.getResources().obtainTypedArray(id);
states.addState(new int[] {android.R.attr.state_pressed},images.getDrawable(1));
states.addState(new int[] {android.R.attr.state_focused},images.getDrawable(1));
states.addState(new int[] { },images.getDrawable(0));
}
return states;
}
而这正是HorizontalScrollView XML:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollSports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:scrollbars="none"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:id="@+id/iwm_sports_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
谢谢你,我的英语道歉。
刚刚面临类似的问题,但在列表中,不幸的是,既不能观察既没有按下,也没有选择bg的列表项与HorizontalScrollView。 – sandrstar
在哪里添加** setStateListDrawable **方法以及如何调用它。 –