安卓:从setSelecton列表突出显示的项目 [英] Android: Highlight an item from List on setSelecton

查看:238
本文介绍了安卓:从setSelecton列表突出显示的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView和放大器; listselector.xml。起初我设置使用setSeletion(位置)的项目。但随着C中的项目该$ C $没有得到突出。它只有突出显示当我点击一个项目。

I have a ListView & listselector.xml. Initially I set the item using setSeletion(position). But with this code the item doesn't get highlighted. It gets highlighted only when I click on an item.

        ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
    timeListView = (ListView) findViewById(R.id.listList);
    timeListView.setAdapter(ad);
    timeListView.setOnItemClickListener(this);      
    timeListView.setSelector(R.drawable.listselector);
    timeListView.setSelection(toSelectPos);

     ============================= 
     XML      
     <ListView android:id="@+id/listList" android:layout_width="match_parent"    android:layout_height="wrap_content"  android:dividerHeight="1dp" >

listselector.xml code

listselector.xml code

     <?xml version="1.0" encoding="UTF-8"?>
     <!-- listselector.xml  -->
     <selector xmlns:android="http://schemas.android.com/apk/res/android">  
<!-- Selected -->
<item android:state_focused="true" android:state_selected="false"
        android:drawable="@drawable/focused"/>

<!-- Pressed -->
<item android:state_selected="true" android:state_focused="false"
        android:drawable="@drawable/selected" />     
      </selector>

以上code亮点全列表视图,我只是想强调(改变背景颜色)的项目。为此,我相信我将不得不创造另一个XML为项目和放大器的项目,并设置选择属性;为完成以上未列出。纠正我,如果我错了。

The above code highlights full listview, I only want to highlight(change bg color) of the item. For that I believe I will have to create another xml for the item and set selector properties for the item & not list as done above. Correct me if am wrong.

的一点是,无论如何只有在点击和setSelection选择的项目应该有所改变。有了上面code,它不setSelection,如何使这种情况发生改变。

The point is, in anyway only the selected item on click and on setSelection should be changed. With the above code, it doesn't change on setSelection, how to make that happen.

我看了看其他的QS,但找不到我找,所以请不要作为重复其他QS标志的地步。

I looked in other Qs, but couldn't find the point I am looking for, so pls don't mark it as a duplicate for other Qs.

感谢

推荐答案

正如你可能已经看到了一些答案在这里堆栈溢出,你不能把选择的,因为它实现了一个视图,以便用户界面有始终联系方式,你可以看到这里 不过,也有一些变通方法,你可以做。

As you probably already have seen in some answers here on stack-overflow, you can not put a View selected because it is implemented so that the User Interface has to be always in touch mode as you can see here However, there are some workarounds That you can do.

您可以设置您的ListView onItemClick具有以下监听器:

You can set your ListView onItemClick with the following listener:

    OnItemClickListener itemClickListener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View v, int pos, long id) {

    if (v == lastSelectedView) {

    v.setBackgroundResource(0);
    lastSelectedView = null;

    } else if (v != lastSelectedView) {



    if (lastSelectedView != null)

        lastSelectedView
            .setBackgroundColor(android.R.color.transparent);

    lastSelectedView = v;

    v.setBackgroundDrawable(v.getContext().getResources()
        .getDrawable(R.drawable.some_row_background));
    }
}

};

在此,你将色彩lisview项目作为想要的。然而,有一个问题。 的意见回收如果您滚动向下或向上将迎来另一种观点认为。您可以取消的观点上滚动,也可以找到一种方法来标记视图。

In this you will colour the lisview item as want. However there is a catch. The recycling of views will mark another view if you scroll down or up. You can "deselect" the view on scroll or you can find a way to tag the view.

最好的问候

这篇关于安卓:从setSelecton列表突出显示的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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