下面的代码是关于制作自定义列表视图,适配器为每个项目创建一个视图“list_view_child”,并且在getView方法中有一个for循环组成3个重复,在这个for循环中我们添加3个新视图“list_item”on “list_items_container”使用方法addView(View v),“list_item”有一个选择器来改变“list_item”视图的背景,这就是问题所在。 如果我点击其中一个视图,他们的背景会一起变化。
显示这些图像以便更好地理解问题:
Pressed_false:http://i44.tinypic.com/2jfaqzb.png
Pressed_true(如果“Item 2& View:1”或“Item 2& View:2”或“Item 2& View:3”被按下):http://i42.tinypic.com/2l97ayw.png
list_view_child XML布局
<?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:background="#d2d2d2"
android:orientation="vertical" >
<TextView
android:id="@+id/child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#ff0000"
android:text="Test" />
<LinearLayout
android:id="@+id/list_items_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
</LinearLayout>
</LinearLayout>
list_item XML布局:
<?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="wrap_content"
android:padding="3dp"
android:orientation="horizontal"
android:background="@drawable/selector"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#ffff00" />
</LinearLayout>
选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed_true" />
<item android:state_pressed="false" android:drawable="@drawable/pressed_false" />
</selector>
pressed_true:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000" />
</shape>
pressed_false:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0000ff" />
</shape>
列表视图适配器类:
public class ListViewAdapter extends BaseAdapter {
private Activity activity;
private HashMap<String, String> data;
public ListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
HashMap<String, String> datapos = new HashMap<String, String>();
datapos = data.get(position);
LinearLayout listitemcontainer = (LinearLayout) vi.findViewById(R.id.list_item_container);
for (int i = 1; i <= 3; i++) {
View listitem = inflater.inflate(R.layout.list_item, null);
TextView itemtitle = (TextView) listitem.findViewById(R.id.item_title);
itemtitle.setText("Item : " + position + " & View : " + i);
LinearLayout.LayoutParams listitemparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
listitemparams.setMargins(0, 15, 22, 0);
listitem.setLayoutParams(listitemparams);
listitemcontainer.addView(listitem);
}
}
}
答案 0 :(得分:0)
如果您的观看次数没有点击监听器,则默认情况下他们将采用其父级的选择状态。为防止这种情况,您可以在list_item上使用此属性:
机器人:duplicateParentState = “假”