如何突出显示ListView中的项目

时间:2017-01-31 07:08:48

标签: android android-layout listview

我正在使用下面的代码,但它不起作用。为什么?

MainActivity:

final SongAdapter songAdt = new SongAdapter(getApplicationContext(), songList);
lv.setAdapter(songAdt);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           view.setSelected(true);
           ...

的ListView:

<ListView android:id="@+id/lvP" android:layout_width="fill_parent"
    android:scrollbars="vertical"
    android:fadeScrollbars="false"
    android:layout_height="0dp" android:layout_weight="0.57"android:divider="#555555"
    android:drawSelectorOnTop="true"
    android:background="@drawable/bg_key" />

bg_key.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    android:id="@+id/myselector"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_focused="false"
        android:drawable="@color/activated_color" />
    <item
        android:state_activated="false"
        android:drawable="@color/activated_color" />
    <item
        android:state_selected="true"
        android:drawable="@color/activated_color" />
    <item
        android:drawable="@color/default_color" />
</selector>

color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="pressed_color">#222222</color>
    <color name="activated_color">#1d1d1d</color>
    <color name="checked_color">#ff0000</color>
    <color name="default_color">#111111</color>
</resources>

编辑:

根据建议,我删除了android:background =&#34; @ drawable / bg_key&#34;从ListView然后我添加到

song_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    tools:context=".MainActivity"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="@drawable/bg_key">


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/song_cover"
        android:scaleType = "fitXY"
        android:src="@drawable/note"
        android:background="#171717"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="1dp" />

    <TextView
        android:id="@+id/song_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:textColor="#fcdb8c"
        android:background="@android:color/transparent"
        android:textStyle="normal"
        android:textSize="16sp"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/song_cover"
        android:layout_toRightOf="@+id/song_cover"
        android:lines="1"
        android:scrollHorizontally="true"
        android:ellipsize="end"
        android:paddingRight="40dp"
        android:paddingLeft="3dp" />

    <TextView
        android:id="@+id/song_duration"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:text="0:00"
        android:textColor="#ffffff"
        android:background="@android:color/transparent"
        android:textSize="11sp"
        android:layout_gravity="center_vertical"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:gravity="center_horizontal|top" />

    <TextView
        android:id="@+id/song_artist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Artist"
        android:textColor="#ffffff"
        android:background="@android:color/transparent"
        android:textSize="14sp"
        android:layout_weight="0.67"
        android:layout_below="@+id/song_title"
        android:layout_toRightOf="@+id/song_cover"
        android:layout_toEndOf="@+id/song_cover"
        android:lines="1"
        android:scrollHorizontally="true"
        android:ellipsize="end"
        android:paddingRight="40dp"
        android:paddingLeft="3dp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

将您的选择器属性更改为此选项,并使用编辑后的Que android:state_focused="true" android:state_activated="true" android:state_selected="true"中的背景。

答案 1 :(得分:0)

在列表项目上使用此单击:

    View rowView = view;
setItemSelected(rowView); //use this inside on item click

 public void setItemSelected(View view){
    View rowView = view;
    TextView tv = (TextView)rowView.findViewById(R.id.textView_two); // try your text view here
    tv.setTextColor(Color.WHITE);
}