【问题标题】:How to Highlight a ListItem in a list view?如何在列表视图中突出显示 ListItem?
【发布时间】:2017-02-10 16:20:33
【问题描述】:

我想突出显示 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>

当我点击或按下项目时它只是改变颜色,如果我移除触摸,它会改变为默认颜色!任何人都可以帮我解决我所缺少的

【问题讨论】:

标签: android listview


【解决方案1】:

state_selected 的尝试案例为假

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

【讨论】:

  • 我会尽力回复你,你能详细说明一下
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    检查我制作的ViewSelector。它使用起来非常简单,可以应用于任何视图。

    ViewSelector viewSelector = new ViewSelector();
    
    viewSelector.onClickColorSelector(relativelayout, Color.RED, 0);
    

    只需在参数中传递一个半透明颜色

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2011-10-06
      • 2012-05-09
      • 1970-01-01
      • 2012-06-10
      • 2013-09-07
      • 2012-01-20
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多