保持片段中所选项目的突出显示? [英] Maintaining highlight for selected item in fragment?

查看:78
本文介绍了保持片段中所选项目的突出显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个片段的布局.左手片段是使用SimpleCursorAdaptor的ListFragment,右手片段中填充有从左片段列表中选择的项的详细信息.我试图弄清楚如何确保从ListFragment 保持中突出显示所选的项目,直到选择了列表中的另一个项目.

I have a layout with two fragments. The left hand fragment is a ListFragment using a SimpleCursorAdaptor, the right hand one is populated with details about the item selected from the list in the left fragment. I'm trying to figure out how to make sure the selected item from the ListFragment stays highlighted until another item in the list is selected.

经过一些研究,我尝试使用android:background="@drawable/item_selector"可以更改不同状态的颜色,但是它们似乎都不会持久.我以为被选中会...似乎合乎逻辑,在您选择其他项目之前,您选择的项目仍会保留为选中的项目.

After some research I got as far as trying to use android:background="@drawable/item_selector" I can changes the colors for the different states, but none of them seem to persist. I thought that selected would... it just seems logical that your selected item remains the selected item until you select a different one.

我什至尝试在我的点击处理程序中使用v.setSelected(true);,希望它可以保持状态,但这也不起作用.

I even tried using v.setSelected(true); in my clickhandler hoping it might maintain the state, but that didn't work either.

我是否缺少状态?我浏览了开发文档,似乎没有其他合适的方法了……

Is there a state I'm missing? I looked through the dev docs and nothing else seemed appropriate...

item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_selected="true" 
        android:drawable="@color/blue" />
</selector>

我不确定还有什么其他代码可以帮助您,所以请随时询问您认为必要的其他内容.

I'm not sure what other code might help, so feel free to ask for whatever else you might think necessary.

推荐答案

答案是经过进一步研究的android文档(实际上是文档的两部分).

The answer was in the android documentation after further research (two parts of the documentation really).

首先,在触摸模式下,没有处于选中或聚焦状态.

First, in touch mode there is no selected or focused state.

第二,默认情况下,列表视图设置为无选择模式(这意味着列表中的任何项目都不能具有选中状态).我要做的就是通过添加以下内容来更改选择模式(它可以是单个或多个,我只需要/需要单个):

Second, by default, listviews are set to a choice mode of none (meaning that no item in the list can have a chosen state). All I had to do was change the choice mode (it could be single or multiple, I only needed/wanted single) by adding this:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

然后我在选择器XML中使用了选择状态(转换为activated状态):

Then I used the choice state in my selector XML (translates to the activated state):

<item 
 android:state_activated="true" 
 android:drawable="@color/blue" />

将其应用在行布局xml文件中作为背景:

Apply that in the row layout xml file as the background:

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:background="@drawable/item_selector"
    android:gravity="center_vertical"/>

我选择的项目现在显示为蓝色背景,直到选择了另一个项目.

My chosen item now appears with a blue background until a different item is chosen.

请注意,此(android:state_activated)需要Android API 11或更高版本.

Note that this (android:state_activated) requires Android API 11 or later.

这篇关于保持片段中所选项目的突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆