【发布时间】:2015-07-11 16:01:19
【问题描述】:
我希望 ListView 中的项目在我触摸时消失。它正在工作,但是 问题是当我滚动列表时。随机项目正在消失或显示。 这是我的代码:
private ListView list;
(...)
list = (ListView) findViewById(R.id.units_list);
adapter = new UnitAdapter(this, R.layout.row_list, units);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
v.setVisibility(View.INVISIBLE);
}
});
列表视图 XML:
<ListView
android:id="@+id/units_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/strut"
android:layout_marginTop="5dp" >
</ListView>
row_list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/img"
android:gravity="center_vertical"
android:textSize="14sp" />
<ImageView
android:id="@+id/upgrade"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical" />
我也试过了:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
list.getChildAt(position).setVisibility(View.INVISIBLE);
}
甚至:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
list.getChildAt((int) id).setVisibility(View.INVISIBLE);
}
但它的工作原理相同。 问题是什么?为什么 View 与多行相关?我该如何解决这个问题?
【问题讨论】:
-
listview 重用可见区域外的项目