2012-09-28 46 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 **方法以及如何调用它。 –

相关问题