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

我有自定义适配器视图的问题.

我尝试,更改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

android:color="@color/cProjectSelected"/>

android:color="@color/cProjectUnSelected"/>

解决方法:

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

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

list_selector.xml

并在列表视图中为xml设置listSelector

android:listSelector="@drawable/list_selector"

color.xml

#000000

#FFFFFF

#a5acb0

#525964

#212121

#a6b1ba

#C9282D

#A9A9A9

#000000

#00000000

#FFFFFF

#00aceb

#8AB8E0

#55000000

#627583

#393430

#1f1c17

#546778

#8E8E8E

#0f5690

对于custom_list_item,布局应该是

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="?android:attr/activatedBackgroundIndicator" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20sp"

android:textStyle="bold" />

and minimum version of your application should be 11

标签:android,java,android-custom-view

来源: https://codeday.me/bug/20190926/1819405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值