0

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

这是我的 xml 文件

选定的.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);
 }
});

请帮忙

4

1 回答 1

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);
            }

翻译.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>

新状态.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 是您要突出显示的列表视图的行。

于 2013-11-12T10:29:02.257 回答