【问题标题】:How to highlight onclick Item of ListView in Xamarin Android如何在 Xamarin Android 中突出显示 ListView 的 onclick 项
【发布时间】:2017-09-24 18:14:28
【问题描述】:

我希望在单击 ListView 中的项目时看到高亮显示,但它不起作用。我用谷歌搜索了很长时间来解决这个问题,但没有任何帮助。

我在 Android 原生中创建列表时没有任何问题。 这是适用于 Android 原生项目的布局 XML。

<ListView
            android:id="@+id/menuList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/img_logo_arkema"
            android:layout_below="@+id/img_logo_paladin"
            android:background="#ffffffff"
            android:choiceMode="singleChoice" />

但是当我在 Xamarin.Android 项目中创建相同的 ListView 时,这不起作用。 这是在 Xamarin.Android 项目上不起​​作用的布局 XML。

<ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/colorTopDivider"
        android:dividerHeight="@dimen/right_menu_divider_size"
        android:choiceMode="singleChoice" />

我正在 Visual Studio 2015 中开发 Xamarin.Android。

感谢您的帮助!

【问题讨论】:

  • 您是否收到任何错误消息?能否请您发布您的适配器的代码?
  • 感谢您的回复,猫王。我没有收到任何错误消息。

标签: android listview xamarin.android


【解决方案1】:

是的,基本上你的想法是正确的Passionate.C。但是您也可以使用选择器更轻松,您在 xml 中定义一个选择器(在 drawble 文件夹内)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />      
</selector>

然后在您的 listView 中调用 setSelector() 并传递您自己的选择器。这是一个更可定制的解决方案,因为您还可以轻松控制 listView 项目的其他状态

【讨论】:

  • 我同意,毛里齐奥。它确实是更可定制的解决方案。但我不想为基本亮点创建新资源。谢谢。
【解决方案2】:

我试了几个小时,终于自己修好了。

问题是我将颜色设置为列表项布局的背景。 当我删除列表项布局中的背景属性时,它起作用了!

这里有更详细的解决方案。

highlighting the selected item in the listview in android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2014-07-09
    • 2012-06-24
    • 1970-01-01
    • 2011-08-16
    • 2013-11-18
    • 2012-01-05
    • 2012-03-30
    相关资源
    最近更新 更多