2012-09-28 40 views
0

我在Horizo​​ntalScrollView和ImageButton(也是ImageView)中存在一個問題。 當Horizo​​ntalScrollView被填充時,我已經動態地分配了一個drawable選擇器。Android ImageView:動態選擇器在Horizo​​ntalScrollView中不起作用

一切都好,因爲您可以點擊並觸發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; 
} 

而這正是Horizo​​ntalScrollView 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> 

謝謝你,我的英語道歉。

回答

1

好的,解決了。我無法相信它!

我已經把 「選擇國家」 的動態選擇:

public static StateListDrawable setStateListDrawable(Context context, TypedArray images){ 

     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[] {android.R.attr.state_selected},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[] {android.R.attr.state_selected},images.getDrawable(1));** 
      states.addState(new int[] { },images.getDrawable(0)); 
     } 

     return states; 
    } 

我改變了對的LinearLayout屬性descendantFocusability:

<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:layout_gravity="center_horizontal"> 

    <LinearLayout 
     android:id="@+id/iwm_sports_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     **android:descendantFocusability="blocksDescendants"**> 

    </LinearLayout> 

</HorizontalScrollView> 

乾杯!

P.D:對不起,我的英語!

+0

剛剛面臨類似的問題,但在列表中,不幸的是,既不能觀察既沒有按下,也沒有選擇bg的列表項與Horizo​​ntalScrollView。 – sandrstar

+0

在哪裏添加** setStateListDrawable **方法以及如何調用它。 –

相關問題