Android ImageView:动态选择器在 HorizontalScrollView 中不起作用

问题描述 投票:0回答:1

我对 HorizontalScrollView 和其中包含的 ImageButton(也是 ImageView)有疑问。当填充 HorizontalScrollView 时,我动态分配了一个可绘制选择器。

一切正常,因为您可以单击并触发 OnClickListener。问题在于项目状态。单击 ImageButton(触摸屏)时,显示正确的可绘制对象,但当我松开手指(非触摸屏)时,显示默认图像而不是按下的图像。 如果我分配一个静态可绘制选择器(XML 选择器),也会出现同样的问题。

这是动态选择器的代码:

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[] { },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>
selector state imagebutton horizontalscrollview
1个回答
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>

干杯!!!

PD:对不起我的英语!!!

© www.soinside.com 2019 - 2024. All rights reserved.