我想从列表中更改单个项目的背景。我的代码将背景更改为所有项目。你能帮忙吗?这是代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="95dp"
android:background="@drawable/friend_bubble"
android:layout_below="@id/tv_message_date"
>
<TextView
android:id="@+id/tv_message_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:focusable="true"
android:clickable="true"
/>
</RelativeLayout>
我的java代码是:
@Override
public boolean onTouch(View view, MotionEvent event) {
RelativeLayout rlBubble = (RelativeLayout)view.findViewById(R.id.tv_message_text).getParent();
Drawable backgroundImage = rlBubble.getBackground();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (backgroundImage == friendBubble ) {
rlBubble.setBackgroundDrawable(friendBubbleSelected);
} else {
rlBubble.setBackgroundDrawable(userBubbleSelected);
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (backgroundImage == friendBubbleSelected) {
rlBubble.setBackgroundDrawable(friendBubble);
} else {
rlBubble.setBackgroundDrawable(userBubble);
}
}
return false;
}