如何在列表视图中突出显示ListItem?

时间:2016-10-07 06:51:09

标签: android listview

我想突出显示list view中的特定行项目,以显示为所选项目。

我有一个国家/地区的列表,我想要突出显示一个国家/地区,以便通过按钮选择它。

我用过这个:

<ListView
    android:id="@+id/list_country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/doneBtn"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/list_selector"
    android:layout_below="@+id/image_logo">
</ListView>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/list_selector">
    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center"
        android:text="CountryName"
        android:textSize="24sp"/>

</LinearLayout>



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        mCountryAdapter = new CountryAdapter(this, R.layout.single_country, countryList);
        mListViewCountry.setAdapter(mCountryAdapter);
        mListViewCountry.setSelector(R.drawable.list_selector);
        clickListeners();
    }

这是我的选择器:

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

</selector>

当我点击或按下项目时,它只是改变颜色,如果我删除触摸,它将更改为默认颜色!任何人都可以帮助我找不到的东西

3 个答案:

答案 0 :(得分:1)

尝试state_selected为false

<item android:state_pressed="false" android:drawable="...."/>

答案 1 :(得分:1)

您可以使用convertView.setAlpha(0.8f);使其略微透明,以区分选定和未选定的项目

答案 2 :(得分:0)

检查ViewSelector我做了。它非常易于使用,可以应用于任何视图。

ViewSelector viewSelector = new ViewSelector();

viewSelector.onClickColorSelector(relativelayout, Color.RED, 0);

在参数

中传递halv透明色
相关问题