【问题标题】:How to set ListView selected item alternet text color in android如何在 android 中设置 ListView 选定项交替文本颜色
【发布时间】:2018-08-22 18:57:38
【问题描述】:

我有一个带有 imageview 和 textview 的自定义列表视图。我希望当用户选择一个项目时,textview 颜色应该改变,所有其他 textview 应该保持默认。

这是我的 xmls

ListView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listViewBell"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

ListViewItems.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TextView
        android:id="@+id/txt_bell_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/rihanna_love_the_way_lie"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/list_bell_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/bronze_bell" />
</RelativeLayout>

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    在drawable中创建如下button_text.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
              android:color="#ffff0000"/> <!-- pressed -->
        <item android:state_focused="true"
              android:color="#ff0000ff"/> <!-- focused -->
        <item android:color="#ff000000"/> <!-- default -->
    </selector>
    

    将文本视图的 textColor 更改为:

      <TextView
            android:id="@+id/txt_bell_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/rihanna_love_the_way_lie"
            android:textColor="@color/button_text" //point to that xml
            android:textSize="15sp"
            android:textStyle="bold"
            android:typeface="sans" />
    

    您可以阅读有关颜色状态列表here

    【讨论】:

    • 以下代码适用于我:&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:color="#fff"/&gt; &lt;item android:state_activated="true" android:color="#fff"/&gt; &lt;item android:color="#000" /&gt; &lt;/selector&gt;
    • 文件必须是 res/color,而不是 res/drawable
    【解决方案2】:

    正如其他人所说,只需更改 onItemClick 方法中的颜色即可。另外:

    @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
    
    
            setItemNormal();
            View rowView = view;
            setItemSelected(rowView);
    
        }
    
    public void setItemSelected(View view){
        View rowView = view;
        TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
        tv.setTextColor(Color.BLUE);
    }
    
    public void setItemNormal()
    {
        for (int i=0; i< mDrawerList.getChildCount(); i++)
        {
            View v = mDrawerList.getChildAt(i);
            //TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
            TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
            txtview.setTextColor(Color.BLACK);
        }
    }
    

    通过这样做,我们将所有项目设置为正常,然后在每次按下项目时对其进行更改。

    【讨论】:

    • 每次点击时都必须遍历所有项目,这是一种不好的做法。只需使用内置功能即可。
    【解决方案3】:

    为此,您必须为特定的 ImageView 设置 OnClickListener,然后在单击适配器类中的 ImageView 时更改 TextView 的颜色。
    或者,

    mList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                TextView tv= view.findViewById(resID); 
                tv.setTextColor(color)
            }
        });
    

    【讨论】:

      【解决方案4】:

      为列表视图实现setOnItemClickListener

      listView.setOnItemClickListener(new OnItemClickListener() {
      
              @Override
              public void onItemClick(AdapterView<?> adapterView, View view, int position,
                      long arg3) {
                  RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
                  TextView textView = (TextView) relativeLayout.getChildAt(0);
                  ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);
      
                  textView.setTextColor(Color.RED);
                 // Perform whatever action for your textView and imageView
      
      
              }
      
          });
      

      【讨论】:

      • 但是如何将其他项目更改为默认颜色
      【解决方案5】:

      在多次尝试在导航视图中设置颜色后,我发现了在列表视图中设置文本颜色的最简单方法,也许它会对某人有所帮助

      res/color/listview_selector.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android" >
          <!-- This is used when the navigation item is selected -->
          <item android:state_enabled="true"
              android:state_activated="true" android:color="@color/colorAccent" />
          <!-- This is the default text color -->
          <item
              android:color="@android:color/black" />
      </selector>
      

      并在 TextView 中设置为 android:textColor

      <TextView
          android:id="@+id/tvItem"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="@dimen/activity_vertical_extra_margin"
          android:textColor="@color/listview_selector"
          android:textSize="@dimen/text_size_normal"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          tools:text="Row item text" />
      

      问题出在正确的状态,在导航视图中使用 android:state_activated

      【讨论】:

        猜你喜欢
        • 2013-11-06
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        相关资源
        最近更新 更多