我想要ListView中的項目消失時,我觸摸它。這是工作,但 問題是當我滾動列表。隨機物品正在消失或顯示。 這是我的代碼: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);
}
});
的ListView 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重用可見區域外的項目 – rainash