突出显示所选项目

时间:2013-11-12 10:03:52

标签: android xml listview android-listview

你好,我有一个奇怪的问题我希望在列表视图中突出显示所选项目

继承我的xml文件

selected.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/header" />
 <item android:state_focused="true" android:drawable="@color/selected" />
</selector>

listitem_background.xml

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

托管ListView的活动

<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:layout_marginBottom="2dp"
android:background="@drawable/listitem_background" >

...

<ListView
    android:id="@+id/carsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/carConfirmBtn"
    android:layout_below="@id/carSelect"
    android:paddingLeft="@dimen/small"
    android:paddingRight="@dimen/small"
    android:listSelector="@drawable/selector" />

</RelativeLayout>

最后定义

的代码片段
carsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
carsList.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
   carsList.setSelection(pos);
 }
});

请帮助

1 个答案:

答案 0 :(得分:0)

在你的获取视图中,在返回视图之前,添加以下代码: - 无论要突出显示哪一行,请修改if语句。翻译突出显示所选行5秒(您可以更改它),颜色慢慢淡化为白色。检查xml

            if(position == 0){
                viewHolderThubnail.relImage.setBackgroundResource(R.drawable.translate);
                TransitionDrawable transition = (TransitionDrawable) viewHolderThubnail.relImage.getBackground();
                transition.startTransition(1000); // 1000 is in milliseconds
                highlightPosition =-1;
            }else{
                /*TransitionDrawable transition = (TransitionDrawable) viewHolderThubnail.relImage.getBackground();
                transition.startTransition(1000);*/
                viewHolderThubnail.relImage.setBackgroundResource(R.color.white);
            }

translate.xml

    <?xml version="1.0" encoding="UTF-8"?>
   <transition xmlns:android="http://schemas.android.com/apk/res/android">
          <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
          <item android:drawable="@drawable/new_state" />
          <item android:drawable="@drawable/original_state" />
   </transition>

new_state.xml

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

original_state.xml

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

如果要突出显示列表视图,则relImage是该行。