【问题标题】:Android ImageView: dynamic selector doesn't work within HorizontalScrollViewAndroid ImageView:动态选择器在 Horizo​​ntalScrollView 中不起作用
【发布时间】:2012-10-02 06:44:54
【问题描述】:

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

一切正常,因为您可以单击并触发 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;
}

这是 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>

提前谢谢你,我的英语道歉。

【问题讨论】:

    标签: selector state imagebutton 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:对不起我的英语!!!

    【讨论】:

    • 刚刚遇到了类似的问题,但在列表中,不幸的是,无法观察到既没有被按下,也没有在使用 Horizo​​ntalScrollView 的列表项上选择 bg。
    • 在哪里添加这个setStateListDrawable方法以及如何调用它。
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多