ListView适配器和焦点状态

时间:2012-07-04 11:35:53

标签: android background focus

我有一些ListView。这是项目视图的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" android:background="@drawable/list_drawable_settings"
              android:layout_height="fill_parent" android:gravity="center_vertical" android:orientation="vertical">
    <LinearLayout
            android:id="@+id/section"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        <com.vk.messenger.views.TextViewMyriadPro android:id="@+id/word"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
     </LinearLayout>
    <RelativeLayout android:layout_width="fill_parent"
                   android:layout_height="wrap_content" android:gravity="center_vertical">
        <LinearLayout android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center_vertical" android:layout_width="fill_parent">
        <com.vk.messenger.views.RoundedConersImageView android:id="@+id/userpic"
                   android:layout_marginLeft="9dip" android:layout_marginRight="9dip"
                   android:layout_marginTop="6dip" android:layout_marginBottom="6dip"
                   android:layout_height="40dip"
                   android:layout_width="40dip"/>
        <com.vk.messenger.views.TextViewMyriadPro android:id="@+id/name"
                  android:layout_toRightOf="@id/userpic"
                  android:textColor="@color/friends_text_color"
                  android:textSize="15dip"
                  android:textStyle="bold"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
        </LinearLayout>
        <ImageView android:id="@+id/online"
                   android:visibility="invisible"
                   android:layout_marginRight="9dip"
                   android:layout_centerVertical="true"
                   android:src="@drawable/online_list"
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:layout_alignParentRight="true"/>
    </RelativeLayout>
</LinearLayout>

当listview的元素聚焦时,主层的背景会发生变化。当主要元素聚焦时,如何更改textview元素的文本颜色?

P.S。抱歉我的英语不好(

2 个答案:

答案 0 :(得分:0)

我这样做的想法是使用表示textview元素颜色的整数变量创建自定义数组适配器。在自定义适配器的getView方法中,您将设置textViews的颜色。像这样:

public class CustomAdapter extends
    BaseAdapter {

private String[] data; //Your data
private Context context;
    private int textViewColor = Color.BLACK; //The color of your textViews with a default value

public CustomAdapter(Context _context, String[] _data) {
    context = _context;
    data = _data;
}

    public void setColor(int _color) {
        textViewColor = color;
    }

    //Use a viewholder if you havent already for more efficient listView.
    static class ViewHolder {
    protected TextView textView;
}

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

          viewHolder.textview.setTextColor(textViewColor); //Set the color of your textViews
          //Do anything else you are supposed to...
   }

  ....
}

然后在listView上设置OnFocusChangeListener。在那里,设置textViews的新颜色。像这样:

listView.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    CustomAdapter adapter = (CustomAdapter) listView.getAdapter();
                    adapter.setColor(Color.White); //set the new color of your textViews components
                    adapter.notifyDataSetChanged(); //Call this to update the listview

                }
            });

如果只想更改listView中所选项目的textView的颜色,则需要将onFocusChangedListener添加到适配器中getView()方法的listViewItem。像这样:

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }


          //set the onFocusChangedListener in the View item of the listView
          v.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                TextView textView = (TextView) v.findViewbyId(R.id.your_textViewId);
                if(hasFocus) {
                      textview.setTextColor(Color.BLUE);
                }
                else {
                      textview.setTextColor(Color.BLACK);
                }

            }
        });

          //Do anything else you are supposed to...
   }

答案 1 :(得分:0)

这对于stateful colors来说非常简单:

创建&#34;颜色列表&#34;文件。例如res/color/font_color_selector.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="@android:color/holo_red_light" />
    <item android:state_selected="true"
          android:color="@android:color/holo_blue_light" />
    <item
        android:color="@android:color/white" />
</selector>

然后在你的布局中:<TextView android:textColor="@color/font_color_selector" ... />