【问题标题】:How to change color of ListView items on focus and on click如何在焦点和单击时更改 ListView 项目的颜色
【发布时间】:2011-05-13 22:34:31
【问题描述】:

我的应用中有一个列表视图(这是 xml 布局):

   <?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/arrayList"
       android:layout_width="fill_parent"
android:layout_height="fill_parent"
       android:textFilterEnabled="true"
       android:scrollbars="vertical"
       android:drawSelectorOnTop="true">
</ListView>

我的列表View的每一项都是由两个TextView组成的:

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout android:layout_width="fill_parent"
       xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_container"
       android:padding="5px" android:layout_height="wrap_content"
       android:background="@color/white" android:shrinkColumns="0">
               <TableRow>
               <TextView android:layout_height="wrap_content"
                       android:layout_width="wrap_content" android:layout_below="@+id/
description"
                       android:id="@+id/description"
                       android:textColor="@color/black"
                       android:scrollHorizontally="true"
                       android:singleLine="true"></TextView>
       </TableRow>
       <TableRow>
               <TextView android:layout_width="wrap_content"
                       android:layout_height="wrap_content" android:id="@+id/result"
                       android:textColor="@color/grey"
                       android:maxLines="1"
                       android:scrollHorizontally="true"></TextView>
       </TableRow>

</TableLayout>

我从一个 ArrayAdapter 中填充我的 listView,以这种方式:

public class Matches extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    //set layout

    setContentView(R.layout.list_layout);
  // obtain reference to listview
  ListView listView = (ListView) findViewById(R.id.arrayList);

  ArrayAdapter<Match> arrayAdapter = new ArrayAdapter<Match>(
        this, R.layout.custom_row, R.id.description, createItems()) {

     @Override
     public View getView (int position, View convertView, ViewGroup parent){
        Match item = getItem (position);
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.custom_row, null);
        TextView description = (TextView)rowView.findViewById(R.id.description);
        TextView result = (TextView)rowView.findViewById(R.id.result);
        description.setText(item.description + "  Risultato: " + item.result );
        result.setText(item.date + "  " + item.hour);
        return rowView;
     }
  };

  listView.setAdapter(arrayAdapter);

我的目标是能够在选择或按下父视图时更改这些子视图的文本颜色和背景。

我该怎么做?

【问题讨论】:

  • 如何在没有用户点击的情况下设置焦点?

标签: android listview background-color listviewitem


【解决方案1】:

无论何时选择父行,列表行中的子视图都应被视为已选中,因此您应该能够在要更改的视图上设置正常状态的可绘制/颜色列表,而无需使用凌乱的 Java 代码.见this SO post

具体来说,您可以将 textView 的 textColor 设置为这样的 XML 资源:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 

【讨论】:

  • 嗨,Yoni,setItemsCanFocus(boolean) 是什么意思?
【解决方案2】:

在您的 main.xml 中,在您的 ListView 中包含以下内容:

android:drawSelectorOnTop="false"

android:listSelector="@android:color/darker_gray"

【讨论】:

  • 如果你这样做,焦点列表单元格在释放触摸后保持突出显示。
【解决方案3】:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >    
    <item android:state_pressed="true" android:drawable="@drawable/YOUR DRAWABLE XML" /> 
    <item android:drawable="@drawable/YOUR DRAWABLE XML" />
</selector>

【讨论】:

  • 嘿 Dhiral,setItemsCanFocus(boolean) 是什么意思?
【解决方案4】:

这里有一个good article,介绍如何将选择器与列表一起使用。

与其将它设置为ListView的android:background,我相信你想设置android:listSelector如下所示:

<ListView android:id="@+id/list" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:layout_gravity="center"
  android:divider="@null" 
  android:dividerHeight="0dip"
  android:listSelector="@drawable/list_selector" />

【讨论】:

    【解决方案5】:
    listview.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, View view,
                    final int position, long id) {
                // TODO Auto-generated method stub
    
                 parent.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.listlongclick_selection));
    
                return false;
            }
        });
    

    【讨论】:

    • Sir Many Places Create Null Pointer exception.
    【解决方案6】:

    很老了,但我一直在努力解决这个问题,这就是我在纯 xml 中解决它的方法。在 res/values/colors.xml 我添加了三种颜色(colour_...);

    <resources>
    
        <color name="black_overlay">#66000000</color>
    
        <color name="colour_highlight_grey">#ff666666</color>
        <color name="colour_dark_blue">#ff000066</color>
        <color name="colour_dark_green">#ff006600</color>
    
    </resources>
    

    res/drawable 文件夹中,我创建了 listview_colours.xml,其中包含;

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/colour_highlight_grey" android:state_pressed="true"/>
        <item android:drawable="@color/colour_dark_blue" android:state_selected="true"/>
        <item android:drawable="@color/colour_dark_green" android:state_activated="true"/>
        <item android:drawable="@color/colour_dark_blue" android:state_focused="true"/>
    </selector>
    

    main_activity.xml中找到List View并将drawable添加到listSelector

    <ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@drawable/listview_colours"
        android:background="#ff222222"/>
    </LinearLayout>
    

    玩弄 listview_colours.xml 中的 state_... 项以获得您想要的效果。

    还有一种方法可以设置列表视图的样式,但我从来没有设法让它工作

    【讨论】:

      【解决方案7】:

      在您的 setOnClickListener 或您想在列表项上应用的任何内容之外将列表项组件声明为最终组件,如下所示:

      final View yourView;
      final TextView yourTextView;
      

      在覆盖 onClick 或您使用的任何方法时,只需根据需要设置颜色,如下所示:

      yourView.setBackgroundColor(Color.WHITE/*or whatever RGB suites good contrast*/);
      yourTextView.setTextColor(Color.BLACK/*or whatever RGB suites good contrast*/);
      

      或者没有最终声明,假设您为自定义适配器实现了 onClick() 以填充列表,这就是我在 getView() 中为我的 setOnClickListener/onClick() 使用的:

      //reset color for all list items in case any item was previously selected
      for(int i = 0; i < parent.getChildCount(); i++)
      {
        parent.getChildAt(i).setBackgroundColor(Color.BLACK);
        TextView text=(TextView) parent.getChildAt(i).findViewById(R.id.item);
        text.setTextColor(Color.rgb(0,178,178));
      }
      //highlight currently selected item
       parent.getChildAt(position).setBackgroundColor(Color.rgb(0,178,178));
       TextView text=(TextView) parent.getChildAt(position).findViewById(R.id.item);
       text.setTextColor(Color.rgb(0,178,178));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 2011-07-19
        • 2016-06-22
        • 1970-01-01
        相关资源
        最近更新 更多