问题描述
我有蒙山自定义适配器视图的一个问题。我尝试,在变化的背景
事件。我有视图
点击 AdapterView.OnItemClickListener
,从哪里获得所选的项目,并要求 myListView.invalidate();
无效后,调用适配器getView(...)
。在这里,$ C $下这样的:
@覆盖 公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
ProjectAdapterData项目项;
如果(行== NULL){
LayoutInflater充气= LayoutInflater.from(上下文);
行= inflater.inflate(R.layout.project_small_item_layout,NULL);
ProjectAdapterData projectAdapterData =新ProjectAdapterData();
row.setTag(projectAdapterData);
名称=(TextView中)row.findViewById(R.id.txtObjectName);
如果(目标数据[位置] .Name点!= NULL)
name.setText(目标数据[位置] .Name点);
联系地址=(TextView中)row.findViewById(R.id.txtObjectAdress);
如果(目标数据[位置] .Adress!= NULL)
adress.setText(目标数据[位置] .Adress);
}
其他 {
背景=(RelativeLayout的)row.findViewById(R.id.rlProjectBackground);
如果(目标数据[位置] .isSelected)
background.setBackgroundColor(context.getResources()的getColor(R.color.cProjectSelected));
其他
background.setBackgroundResource(R.color.cProjectUnSelected); //它的电话,但没有结果
row.invalidate();
}
返回行;
}
我的问题,为什么背景不发生变化?
我的selector_list
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目安卓state_selected =真
机器人:颜色=@色/ cProjectSelected/>
<项目安卓state_selected =假
机器人:颜色=@色/ cProjectUnSelected/>
< /选择器>
您可以使用选择器高亮显示项目
在绘制文件夹中创建一个XML文件
list_selector.xml
< XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android机器人:exitFadeDuration =@安卓整数/ config_mediumAnimTime>
<项目机器人:可绘制=@色/蓝色的机器人:state_activated =真/>
<项目机器人:可绘制=@色/蓝色的机器人:state_selected =真/>
<项目机器人:可绘制=@色/透明/>
< /选择器>
和设置listSelector以XML为您的ListView像
安卓listSelector =@可绘制/ list_selector
color.xml
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=黑与GT;#000000< /彩色>
<颜色名称=白>#FFFFFF< /彩色>
<颜色名称=light_grey>#a5acb0< /彩色>
<颜色名称=棕色>#525964< /彩色>
<颜色名称=dark_grey>#212121< /彩色>
<颜色名称=AQUA>#a6b1ba< /彩色>
<颜色名称=red_cherry>#C9282D< /彩色>
<颜色名称=银>#A9A9A9< /彩色>
<颜色名称=黑>#000000< /彩色>
<颜色名称=透明>#00000000< /彩色>
<颜色名称=白>#FFFFFF< /彩色>
<颜色名称=蓝色>#00aceb< /彩色>
<颜色名称=spiritclips_bck>#8AB8E0< /彩色>
<颜色名称=translucent_black>#55000000< /彩色>
<颜色名称=grid_bck>#627583< /彩色>
<颜色名称=灰色>#393430< /彩色>
<颜色名称=dark_grey_bg>#1f1c17< /彩色>
<颜色名称=login_font_color_1>#546778< /彩色>
<颜色名称=login_font_color_2>#8E8E8E< /彩色>
<颜色名称=blue_txt>#0f5690< /彩色>
< /资源>
有关custom_list_item的布局应该是
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:背景=机器人:ATTR / activatedBackgroundIndicator>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =20SP
机器人:TEXTSTYLE =黑体/>
< / LinearLayout中>
I have a problem whith custom adapter view.I try, change background of view
on Click
event.I have AdapterView.OnItemClickListener
, where i get selected item, and calling myListView.invalidate();
After invalidate, calling adapters getView(...)
. Here code for this:
@Override 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;
}
My question, why background doesn't change?
My selector_list
<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>
you can use selector to highlight item
In drawable folder create a xml file
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>
and set listSelector in xml for your listview like
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>
for custom_list_item the layout should be
<?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>
这篇关于自定义适配器,选择的项目背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!