簡體   English   中英

更改所選列表視圖項的顏色

[英]change color of selected listview item

我想在按下時更改列表項的顏色

為此,我在下面做了,

list_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    Selected 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/list_focused"/> 

  Pressed
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/list_selected" />  

</selector> 

我在colors.xml中設置了顏色,如下所示,

 <drawable name="list_focused">#36C170</drawable>
  <drawable name="list_selected">#9EC136</drawable>

在我的ListView我這樣寫,

<ListView
            android:id="@+id/list_centers_complete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:cacheColorHint="@android:color/transparent"
            android:listSelector="@drawable/list_item_selector" />

但是當我點擊列表項時,整個背景顏色改變而不是僅列表項。

我怎么解決這個問題? 有什么辦法嗎?

謝謝

"@drawable/list_item_selector"應用於該列表行(列表項)而不是列表本身..

像你的列表項(列表行)..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/list_item_selector">
        <TextView       android:id="@+id/textForList"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:padding="10sp"  />
.
.
.
</LinearLayout>

list_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
    <shape>
        <solid android:color="#66FFFFFF" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#FF666666" />
    </shape>
</item>

</selector>

您應該將選擇器設置為行而不是列表視圖本身。

<item android:state_activated="true">
  <shape android:shape="rectangle">
    <solid android:color="#333333" />
    <padding android:left="5dp" android:right="5dp" />
  </shape></item>
<item><shape android:shape="rectangle">
        <solid android:color="#222222" />
    </shape></item>

嘗試使用自定義適配器,這也可以幫助您完全控制項目並設置選擇的默認項目; listView XML和item XML沒有特殊設置。

public class ListAdapter extends ArrayAdapter<MyObj> {

private final int layoutInflater;
private Context context;
private  List<MyObj> items;
private int mSelectedItem = 0;
private int TAG_UNSELECTED = 0;
private int TAG_SELECTED = 1;

public ListAdapter(Context context, int resource, List<MyObj> items) {
    super(context, resource, items);
    this.context = context;
    this.layoutInflater = resource;
    this.items = items;
}

public void selectItem(int position) {
    mSelectedItem = position;
    notifyDataSetChanged();
}


@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position == mSelectedItem ? TAG_SELECTED : TAG_UNSELECTED;
}


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

    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(layoutInflater, null);
    }

    MyObj myObj = items.get(position);
    TextView textView = (TextView) v.findViewById(R.id.title);
    textView.setText(myObj.title);

    int type = getItemViewType(position);
    if(type == TAG_SELECTED) {
        v.setBackgroundColor(Color.parseColor("#1da7ff"));
        textView.setTextColor(Color.parseColor("#ffffff"));
    } else {
        v.setBackgroundColor(Color.parseColor("#f8f8f8"));
        textView.setTextColor(Color.parseColor("#474747"));
    }

    return v;
}

}

然后在你的活動中:

            ListView listView = (ListView) findViewById(R.id.list_view);
            ListAdapter adapter = new ListAdapter(mContext, R.layout.item_layout, list);
            listView.setAdapter(adapter);
            adapter.selectItem(0); // Default selected item

            // Get selected item and update its background
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    adapter.selectItem(position);
                }
            });

用這個

android:background="@drawable/list_item_selector""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM