如何改变ListView的项目颜色焦点和点击 [英] How to change color of ListView items on focus and on click

查看:143
本文介绍了如何改变ListView的项目颜色焦点和点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表查看我的应用程序(这是XML布局):

i have a list View in my app (this is the xml layout):

   <?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>

我的列表查看每个项目由两个TextView的:

Each item of my list View is composed of two 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,以这种方式:

I'm populating my listView from an ArrayAdapter, in this way:

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);

我的目标是能够改变文字颜色,每当家长选择或pressed。

My goal is to be able to change the text color and the backgorund of these child views whenever the parent is selected or pressed.

我怎样才能做到这一点?

How can i do it?

推荐答案

在列表行的子视图只设置一个正常的状态下,应考虑选择每当父行被选中,所以你应该能够绘制/ color-你想改变,没有凌乱的Java code必要的视图列表。请参阅<一href="http://stackoverflow.com/questions/3506319/android-linearlayout-with-color-resource-what-am-i-doing-wrong">this SO帖子。

The child views in your list row should be considered selected whenever the parent row is selected, so you should be able to just set a normal state drawable/color-list on the views you want to change, no messy Java code necessary. See this SO post.

具体,你会设置文字颜色您textViews对XML资源像这样的:

Specifically, you'd set the textColor of your textViews to an XML resource like this one:

<?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> 

这篇关于如何改变ListView的项目颜色焦点和点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆