开发者

Changing color of custom listview item on focus

开发者 https://www.devze.com 2023-02-11 23:07 出处:网络
I have created a custom list and managed to change the colors of the child items in normal and clicked view. But When I apply it to the focus view, then nothing happens and the default background colo

I have created a custom list and managed to change the colors of the child items in normal and clicked view. But When I apply it to the focus view, then nothing happens and the default background color only gets displayed with the changed color of the text from white to black. Here are the xml files that I am using for what I have done so far.

Layout for listview (listview.xml)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:padding="18sp"
    android:background="开发者_JAVA技巧@drawable/backgroundselector">
    <ImageView android:id="@+id/imageView1"
        android:layout_height="fill_parent" android:layout_gravity="center_vertical"
        android:layout_width="wrap_content" />
    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingLeft="10sp" android:textSize="20sp" />
</LinearLayout>

clicked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ffc61212" />
 </shape>

focussed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff26e0d7" />
 </shape>

normal.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff0a4d66" />
 </shape>

and finally backgroundselector.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/focussed" /> <!-- focused -->
    <item android:state_pressed="true" android:drawable="@drawable/clicked" /> <!-- pressed -->
    <item android:drawable="@drawable/normal" /> <!-- default -->
</selector> 

Please I need help on how can I change the color of the child item of my list view when a child item is focussed...


My advice is to override ListView.onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) method, which will be called when an item is clicked.

And in this method, you can create a new ArrayAdapter. Like this: ArrayAdapter<String> adpater=new ArrayAdapter<String>(CurrentActivity.this,R.layout.foucs,data); where data is an object array used to provide data for the ListView(generally, we use String[] or ArrayList class).

And then, call ListView.setAdapter(adapter) to show your new style.

Hope it helps. I am a greedhand:)

Ah, I find a new and convenient way to do this.

In ListView.onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) method, just call arg1.setBackgroundColor(newColor); to change the color of the item.

0

精彩评论

暂无评论...
验证码 换一张
取 消