我在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 **方法以及如何調用它。 –