首页 > 解决方案 > gridview listSelector focusableInTouchMode

问题描述

我正在创建一个由 d-pad 和触摸控制的自定义启动器。为了获得对项目的关注,我为列表选择器设置了一个选择器 XML,并在获得焦点时设置了自定义背景。但是,当我在触摸模式下按下该项目时,它也会获得焦点。

我想以某种方式将列表选择器的 focusableInTouchMode 设置为 false,以便在按下时不显示背景。

网格视图 XML:

 <GridView
    android:id="@+id/appsContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:clipToPadding="false"
    android:columnWidth="@dimen/app_row_column_width"
    android:fadingEdge="none"
    android:horizontalSpacing="@dimen/app_row_horizontal_spacing"
    android:numColumns="auto_fit"
    android:overScrollMode="never"
android:listSelector="@drawable/bg_list_selector"
android:focusableInTouchMode="false"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbars="none"
    android:stretchMode="spacingWidth"
    android:verticalSpacing="@dimen/app_row_vertical_spacing"/>

bg_list_selector xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_list_item_focused" android:state_focused="true"/>
<!--<item android:drawable="@drawable/bg_list_item_focused" android:state_hovered="true"/>-->
<item android:drawable="@color/full_transparent" android:state_selected="true"/>
<item android:drawable="@color/full_transparent" android:state_pressed="true"/>
<item android:drawable="@color/full_transparent" android:state_window_focused="true"/>
<item android:drawable="@color/full_transparent" />

标签: androidandroid-studio

解决方案


解决方案:

改为将选择器应用于background项目布局并设置android:listSelector="@null".

网格:

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView 
        android:id="@+id/categories_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:listSelector="@null"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />
</FrameLayout>

物品:

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_view_selector">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

推荐阅读