【问题标题】:setSelection not working - getSelectedItemPosition always returns -1setSelection 不工作 - getSelectedItemPosition 总是返回 -1
【发布时间】:2014-01-01 04:34:02
【问题描述】:

我已经实现了一个导航抽屉。一切正常,除了我无法在选定状态下更改项目的背景。按下时背景会改变,但松开时会恢复正常。

在我的 xml 中,我将 state_pressed 和 state_selected 都设置为 color2,而 normal 是 color1。按下时我得到颜色 2,但释放时返回颜色 1。

所以,我检查了我的班级以找出问题所在,发现 getSelectedItemPosition() 总是返回 -1,尽管位置返回正确(如下所述)。

有什么帮助吗?

编辑 1: 好的,我也添加了 getCheckedItemPosition() 方法,它正确返回正确的位置。但是我的列表视图背景没有改变,虽然我使用了 state_checked。

Log.d("postion",""+position);  // THIS RETURNS AS EXPECTED, LET SAY 2 FOR 3RD ITEM
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
Log.d("Selected",""+mDrawerList.getSelectedItemPosition()); // THIS ALWAYS RETURNS -1
Log.d("Checked",""+mDrawerList.getCheckedItemPosition()); // THIS RETURNS CORRECTLY, e.g. 2

列表视图:

<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/col3"
android:dividerHeight="1dp"       
android:listSelector="@drawable/menu_selector"
android:background="@color/col3"/>

菜单选择器:

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

    <item android:state_selected="true" android:drawable="@drawable/menu_active" />

    <item android:state_checked="true" android:drawable="@drawable/menu_active" />

    <item android:drawable="@drawable/menu_norm" />
</selector>

列出项目

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/menu_selector">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/ic_home"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/white"
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>
</RelativeLayout>

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    您正在尝试从 ListView 中获取所选项目的位置,即使没有选择任何项目。从getSelectedItemPosition() 的文档中可以看到,如果没有选择任何项目,则返回INVALID_POSITION,即-1

    【讨论】:

    • yaa,我知道.. 点是,不应该被上一行选中——mDrawerList.setSelection(position); ?
    • 另外,知道我应该添加什么以使我选择的项目的背景为“grad2”颜色吗?
    【解决方案2】:
    android:listSelector="@drawable/menu_selector"
    

    这是 ListView 用于当前选定项目的可绘制对象。因此,它在 ListView 确定项目的状态为选定状态后使用。因此,您可以将其设置为您的 android:drawable="@color/grad2"

    android:listSelector="@color/grad2"
    android:drawSelectorOnTop="true"
    

    【讨论】:

    • 我已经更新了主要问题中的代码.. 但是,我尝试使用 android:drawSelectorOnTop="true",但没有积极的结果...
    【解决方案3】:

    要使 ListView 突出显示选择,您必须确保:

    1. ListView 设置为单选或多选模式。
    2. 每个列表项的根视图实现Checkable。您可以将CheckedTextView 用于列表项。
    3. 列表项视图的背景设置为具有不同可绘制对象的选择器,用于检查和正常状态。

    【讨论】:

    • 抱歉延迟回复... 1) 选择模式是singleChoice ... 2) 不知道你是什么意思。如果您详细说明.. 3)已经设置(我更新了代码)
    猜你喜欢
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2019-10-10
    • 2014-04-13
    • 2017-03-20
    • 2013-11-04
    • 2011-12-09
    相关资源
    最近更新 更多