【问题标题】:Custom adapter, selected item background自定义适配器,选中项背景
【发布时间】:2014-02-19 18:09:37
【问题描述】:

我对自定义适配器视图有疑问。 我尝试在Click 事件上更改view 的背景。 我有AdapterView.OnItemClickListener,在那里我得到了选定的项目,并调用myListView.invalidate();

无效后,调用adapters getView(...)。这里的代码:

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

    View row = convertView;
    ProjectAdapterData projectItem;


    if (row == null) {

        LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(R.layout.project_small_item_layout, null);

        ProjectAdapterData projectAdapterData = new ProjectAdapterData();

        row.setTag(projectAdapterData);
        name = (TextView)row.findViewById(R.id.txtObjectName);
        if (objectData[position].Name!= null)
            name.setText(objectData[position].Name);
        adress = (TextView)row.findViewById(R.id.txtObjectAdress);
        if (objectData[position].Adress != null)
            adress.setText(objectData[position].Adress);
    }
    else {
        background = (RelativeLayout)row.findViewById(R.id.rlProjectBackground);
        if (objectData[position].isSelected)
            background.setBackgroundColor(context.getResources().getColor(R.color.cProjectSelected));
        else
            background.setBackgroundResource(R.color.cProjectUnSelected); //it's calls, but no result
        row.invalidate();
    }
    return row;
}

我的问题,为什么背景没有改变?

我的选择器列表

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
          android:color="@color/cProjectSelected"/>
        <item android:state_selected="false"
          android:color="@color/cProjectUnSelected"/>
    </selector>

【问题讨论】:

    标签: java android android-custom-view


    【解决方案1】:

    您可以使用选择器突出显示项目

    在drawable文件夹中创建一个xml文件

    list_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    
        <item android:drawable="@color/blue" android:state_activated="true"/>
        <item android:drawable="@color/blue" android:state_selected="true"/>
        <item android:drawable="@color/transparent"/>
    
    </selector>
    

    并在 xml 中为您的列表视图设置 listSelector,例如

    android:listSelector="@drawable/list_selector"
    

    颜色.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <color name="BLACK">#000000</color>
        <color name="WHITE">#FFFFFF</color>
        <color name="light_grey">#a5acb0</color>
        <color name="brown">#525964</color>
        <color name="dark_grey">#212121</color>
        <color name="aqua">#a6b1ba</color>
        <color name="red_cherry">#C9282D</color>
        <color name="silver">#A9A9A9</color>
        <color name="black">#000000</color>
        <color name="transparent">#00000000</color>
        <color name="white">#FFFFFF</color>
        <color name="blue">#00aceb</color>
        <color name="spiritclips_bck">#8AB8E0</color>
        <color name="translucent_black">#55000000</color>
        <color name="grid_bck">#627583</color>
        <color name="grey">#393430</color>
        <color name="dark_grey_bg">#1f1c17</color>
        <color name="login_font_color_1">#546778</color>
        <color name="login_font_color_2">#8E8E8E</color>
        <color name="blue_txt">#0f5690</color>
    
    </resources>
    

    对于 custom_list_item,布局应该是

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="?android:attr/activatedBackgroundIndicator" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    您的应用程序的最低版本应为 11

    【讨论】:

    • 无法从可绘制文件夹访问@color
    • 你应该在 values 文件夹中创建 color.xml 并为你的布局定义颜色
    • 我已经有 color.xml 的值,但无法访问它。我只能访问 android:@drawable
    • 我不会忘记的:) 我拿走了你的选择器版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多