当前位置: 首页 > 知识库问答 >
问题:

自定义适配器,所选项目背景

商冠玉
2023-03-14

自定义适配器视图有问题。我试着在点击事件上更改视图的背景。我有AdapterView。OnItemClickListener,从中获取所选项目,并调用myListView。使无效

无效后,调用适配器getView(...)。以下代码:

@覆盖公共视图getView(int位置、视图转换视图、视图组父视图){

    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>

共有1个答案

公西嘉玉
2023-03-14

您可以使用选择器高亮显示项目

在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中为您的listview设置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>

对于自定义列表项,布局应为

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

 类似资料:
  • 编辑:选定项目的蓝色文本。

  • 英文原文:http://emberjs.com/guides/models/customizing-adapters/ 在Ember Data中,处理与后台数据仓库通信的逻辑是通过Adapter来完成的。Ember Data适配器内置了一些关于REST API的假定。如果后台的实现与Ember Data假定的惯例不同,那么通过扩展缺省的适配器可能很容易的实现。 有时因为一些原因需要自定义适配器,例

  • Ember.js适配器指定如何在后端数据存储中保存数据,例如URL格式和REST API标头。 默认的Ember适配器包含一些REST API的内置假设。 这些假设有助于更轻松,更好地构建Web应用程序。 可以使用以下命令创建适配器 - ember generate adapter adapter-name 运行上面的命令时,它将显示以下行 - import DS from 'ember-dat

  • 问题内容: 我正在扩展BaseAdapter以创建自定义listview行。我有上下文菜单,每当用户按住该行时就会打开,并提示他是否要删除它。但是,如何删除行?哈希图仅是测试数据。 问题答案: 您不要从适配器中删除!您从项目中删除!适配器位于您的商品和视图之间。您可以从视图中获得职位,并根据职位可以删除项目。然后,适配器将刷新您的视图。 那意味着你需要做这样的事情

  • 我有一个清单,我在回收商的视图中展示了这一点 一些项目有蓝色背景和其他项目有灰色背景 我想编辑所选项目背景(所选项目表示已单击的项目) 这是我的适配器类

  • 主要内容:创建自定义适配器,注册自定义适配器,使用适配器Gson使用其内置适配器执行对象的序列化/反序列化。 它也支持自定义适配器。 让我们来讨论如何创建一个自定义适配器以及如何使用它。 创建自定义适配器 通过扩展类并传递目标类型的对象来创建自定义适配器。 重写读写方法分别执行自定义的反序列化和序列化。 注册自定义适配器 使用注册自定义适配器并使用创建一个Gson实例。参考以下实现代码 - 使用适配器 Gson现在将使用自定义适配器将Json文本转换为