2012-07-04 84 views
1

我有一些ListView。這是項目視圖的代碼: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.對不起,我的英語不好(

回答

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。事情是這樣的:

​​
+0

我認爲這個代碼更改所有TextView的顏色?但我只需要在重點列表項目中更改textview。 – Anton

+0

@Anton我編輯了我的答案,做你需要的。查看最後一段代碼。 – Angelo

0

這是真正的簡單與stateful colors

創建 「顏色列表」 文件。例如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" ... />