【问题标题】:ListView selector changes whole listview not the rowListView 选择器更改整个列表视图而不是行
【发布时间】:2014-01-26 00:11:23
【问题描述】:

我在这里阅读了大量关于列表视图选择器的文章,但我就是无法让它发挥作用。当我在列表视图中按下一行时,整个列表视图而不是所选项目会改变颜色。我正在使用 Listview 片段和自定义行。

首先,我的自定义行是这样开始的,样式设置我的默认背景颜色和其他我想要的东西这里只是一些视图。如左图所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/darkBackdropList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

接下来,我有一个选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
  android:state_pressed="true" 
  android:drawable="@drawable/selected"/> 
</selector> 

drawable/selected 只是浅灰色/蓝色:

<drawable name="selected">#8FBDAB</drawable>

在我的片段中,我这样做:

public class GuideListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getListView().setSelector(R.drawable.darklistselector);
}
}

当我的列表视图显示时,一切看起来都很好。但是,当我选择一行时,覆盖整个活动的列表视图会将颜色更改为“darklistselector”。

我尝试将其设置为自定义行背景,但在黑色、灰色/蓝色和白色之间切换整个屏幕变得更加奇怪。

感谢您的帮助。

【问题讨论】:

    标签: android-listview android-selector


    【解决方案1】:

    对不起,我的错,但万一有人和我做同样的事情。基本上我不得不删除我的样式并将其更改为:

    android:background="@drawable/darklistselector"
    

    然后为未按下添加一个新的选择器:

        <item android:state_pressed="true" android:drawable="@drawable/selected"/> 
        <item android:state_pressed="false" android:drawable="@drawable/standard"/> 
    

    selected 是行高亮,standard 是我以前的样式的正常背景。

    【讨论】:

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