【问题标题】:change color of selected listview item更改所选列表视图项的颜色
【发布时间】:2011-12-09 08:41:17
【问题描述】:

我想改变列表项按下时的颜色

为此,我确实喜欢下面,

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" />

但是当我点击列表项时,整个背景颜色都会改变,而不仅仅是列表项。

我该如何解决这个问题? 有什么办法吗?

谢谢

【问题讨论】:

  • 您正在使用自定义 ListView ?.. 如果是,那么您必须将 android:background="@drawable/list_item_selector" 应用于您的 row.xml 文件的根布局..并从您的 ListView 中删除 android:listSelector 标记.

标签: android listview selector


【解决方案1】:

将"@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>

【讨论】:

  • 我的每个 itam 由 3 个 TextViews 组成,那么我将把它放在哪里?在线性布局中??
  • 创建线性布局。将所有三个文本视图嵌套在此线性布局中,然后将选择器设置为线性布局。
  • 作为背景?像 android:background="@drawable/list_item_selector" ??
  • android:drawable="#FF666666" 这将显示错误,这就是我放入colors.xml的原因
  • @user370305 你能帮我解决这个问题吗stackoverflow.com/questions/18099756/…
【解决方案2】:

您应该将选择器设置为行而不是列表视图本身。

【讨论】:

【解决方案3】:

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

【讨论】:

    【解决方案4】:

    尝试使用自定义适配器,这也有助于您完全控制您的项目并设置一个默认项目; 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);
                    }
                });
    

    【讨论】:

      【解决方案5】:

      使用这个

      android:background="@drawable/list_item_selector""
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 2021-02-05
        • 2015-01-13
        • 2017-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多