gridview listSelector focusableInTouchMode

时间:2019-12-11 12:41:02

标签: android android-studio

我正在创建一个由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" />

1 个答案:

答案 0 :(得分:0)

解决方案:

将选择器应用于项目布局的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>