Android ListView适配器和焦点状态

Android ListView适配器和焦点状态,android,background,focus,Android,Background,Focus,我有一些列表视图。这是项目视图的代码: <?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"

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

<?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元素的文本颜色


另外,很抱歉我的英语不好(

我这样做的想法是创建一个自定义数组适配器,其中包含一个表示textview元素颜色的整数变量。在自定义适配器的
getView
方法中,您将设置textview的颜色。如下所示:

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

                }
            });
@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...
   }
然后在listView上设置一个
OnFocusChangeListener
。在这里,设置文本视图的新颜色。类似如下:

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

                }
            });
@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...
   }
如果只想更改listView中所选项目的textView的颜色,则需要将onFocusChangedListener添加到适配器中
getView()
方法的listViewItem中。类似如下:

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

                }
            });
@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...
   }
这一点非常简单:

创建“颜色列表”文件。例如
res/color/font\u color\u 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颜色?但我只需要更改聚焦列表项中的textView。@Anton我编辑了我的答案,以执行您需要的操作。请参阅最后一段代码。