背景色设置为在Android中选定的ListView项设置为、背景色、ListView、Android

2023-09-05 10:17:57 作者:嘴角依旧上扬的微笑-

我有一个ListView它显示几个项目。现在,我想滚动到一些具体的项目(如第33项)。我知道,这可以通过

完成

  myList.setSelection(32);
 

不过在UI上的项目不接受任何高亮(因为它在触摸模式?!)。如何申请一个特定的背景色为这个项目?我试过

  myList.getSelection()getSelectedView()setBackgroundColor(Color.Red)。
 

但我得到一个NullPointerException异常,因为getSelectedView()返回null。有没有一种方法来实现这一突出?我必须以某种方式通知用户哪些项目是积极的一...

解决方案 要突出选择的项目,你应该要求重点用于ListView和运行setSelection()在UI线程。这为我工作:

  runOnUiThread(新的Runnable(){
    公共无效的run(){
        myList.requestFocus();
        myList.setSelection(位置);
    }
});
 
Android Studio 设置背景颜色

2.To应用特定颜色的项目。您可以使用自定义的listItem。

一个。设置自定义lisItem您的ListView:

  ArrayAdapter<字符串> listAdapter =新的ArrayAdapter<字符串>(这一点,R.layout.customizedlistitem,arrListView);
myList.setAdapter(listAdapter);
 

乙。在您的布局文件夹,创建customizedlistitem.xml

< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:ID =@ + ID / customizedlistitem     机器人:layout_width =match_parent     机器人:layout_height =WRAP_CONTENT     机器人:背景=@可绘制/ customizedbackground/>

℃。在您绘制文件夹,创建customizedbackground.xml是这样的:

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_selected =真正的机器人:STATE_ pressed =假机器人:可绘制=@色/ RED>< /项目>
    <项目的android:STATE_ pressed =真正的机器人:可绘制=@色/ GREEN>< /项目>
    <项目机器人:可绘制=@彩色/黑白>< /项目> <! - 其他国家 - >
< /选择器>
 

Ð。确保颜色红色,绿色和黑色是在你的项目中定义(您可以在值文件夹中color.xml定义它):

< XML版本=1.0编码=UTF-8&GT?; <资源>      <颜色名称=RED>#FF0000< /彩色>      <颜色名称=绿色>#008000< /彩色>      <颜色名称=黑与GT;#000000< /彩色> < /资源>

I have a listview which displays several items. Now I want to scroll to some specific item (e.g. the 33th item). I know that this can be done via

myList.setSelection(32);

But on the UI the item doesn't receive any highlighting (because it is in touch mode?!). How can I apply a specific background color for this item? I tried

myList.getSelection().getSelectedView().setBackgroundColor(Color.Red);

but i get a NullPointerException because getSelectedView() returns null. Is there a way to achieve this highlighting? I have to notify the user somehow about which item is the "active" one...

解决方案

To highlight the selected item, you should request focus for the listView and run setSelection() in the UI thread. This worked for me:

runOnUiThread(new Runnable() {
    public void run() {
        myList.requestFocus();
        myList.setSelection(position);
    }
});

2.To apply a specific color to an item. You can use customized listItem.

a. Set customized lisItem for your listView:

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.customizedlistitem,arrListView);
myList.setAdapter(listAdapter);

b. In your layout folder, create customizedlistitem.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/customizedlistitem"   
    android:layout_width="match_parent"   
    android:layout_height="wrap_content"
    android:background="@drawable/customizedbackground"/>   

c. In your drawable folder, create customizedbackground.xml like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/RED"></item>
    <item android:state_pressed="true" android:drawable="@color/GREEN"></item>      
    <item android:drawable="@color/BLACK"></item>   <!-- for other state -->        
</selector>

d. Make sure color RED, GREEN and BLACK was defined in your project (you can define it in color.xml under values folder):

<?xml version="1.0" encoding="utf-8"?>

<resources>  
     <color name="RED">#FF0000</color>
     <color name="GREEN">#008000</color>
     <color name="BLACK">#000000</color>
</resources>