【问题标题】:Highlight selected item突出显示所选项目
【发布时间】:2013-11-12 10:03:51
【问题描述】:

你好,我遇到了奇怪的问题我想在列表视图中突出显示所选项目

这是我的 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);
 }
});

请帮忙

【问题讨论】:

  • 能否请您提供您想要展示的图片...
  • 尝试从ListView中删除android:listSelector="@drawable/selector"..
  • 我更新问题以保持简单我的意思:)

标签: android xml listview android-listview


【解决方案1】:

在您的获取视图中,在返回视图之前,添加以下代码:- 无论您要突出显示哪一行,修改 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2011-04-19
    • 1970-01-01
    相关资源
    最近更新 更多