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

java– 自定义适配器,所选项目背景,第1张

概述我有自定义适配器视图的问题.我尝试,更改Click事件的视图背景.我有AdapterView.OnItemClickListener,我得到所选项目,并调用myListView.invalidate();无效后,调用适配器getView(…).这里是代码:@覆盖    publicViewgetView(intposition,ViewconvertView,ViewGroupparent

我有自定义适配器视图的问题.
我尝试,更改Click事件的视图背景.
我有AdapterVIEw.OnItemClickListener,我得到所选项目,并调用myListVIEw.invalIDate();

无效后,调用适配器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_List
        

    <selector xmlns:androID="@R_404_6822@://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>

解决方法:

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

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

List_selector.xml

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="@R_404_6822@://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"

color.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="@R_404_6822@://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>

and minimum version of your application should be 11

总结

以上是内存溢出为你收集整理的java – 自定义适配器,所选项目背景全部内容,希望文章能够帮你解决java – 自定义适配器,所选项目背景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存