【问题标题】:Listview item got highlighted on click (not wanted)Listview 项目在单击时突出显示(不需要)
【发布时间】:2015-02-17 21:03:25
【问题描述】:

我有一个包含项目的 TabHost,我希望它们仅在单击时突出显示。

我以前有过它,但我在 ListViews 本身上没有改变任何东西,但在 TabHost 上(我认为这应该没关系..)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="at.htl3r.appmosphere.MainActivity$PlaceholderFragment" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

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

                <ListView
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

我阅读了一些如何禁用它的帖子,但 I don't want to disable it complete 因为我将处理点击操作

我尝试添加choiceMode="none"selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/darkgreen_light" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent" android:state_activated="true"/>
    <item android:drawable="@android:color/transparent" android:state_checked="true"/>
    <item android:drawable="@android:color/transparent" android:state_selected="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

更新

更好的选择:ViewPagerIndicator

【问题讨论】:

    标签: android listview android-fragments android-tabhost


    【解决方案1】:

    您应该将列表视图的选择模式保持为单选(以便能够检测项目选择/点击),并将列表选择器覆盖为透明(@null 适用于旧版本的 android,但不适用于 16+):

    <style name="YourTheme.ListView" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">@drawable/listSelector</item>
        <item name="android:choiceMode">singleChoice</item>
    </style>
    
    <?xml version="1.0" encoding="utf-8"?>
    

    您的 listSelector.xml 可能如下所示:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/darkgreen_light" android:state_pressed="true"/> <!-- pressed -->
        <item android:drawable="@color/darkgreen_light" android:state_focused="true"/> <!-- focused -->
        <item android:drawable="@android:color/transparent"/> <!-- default -->
    
    </selector>
    

    更新

    如果您不想要任何关于项目点击的视觉反馈,只需将列表视图样式中的@drawable/listSelector 更改为@android:color/transparent

    【讨论】:

    • 我只想处理点击而不是选择;没有改变任何东西......仍然选择点击的项目......
    • 我的意思是它不应该是恒定的,如果我点击它,它应该只在新闻期间突出显示,而不是之后
    猜你喜欢
    • 2021-04-02
    • 2018-12-30
    • 2012-09-20
    • 2012-03-06
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 2012-03-13
    • 2013-05-29
    相关资源
    最近更新 更多