2015-07-11 16 views
2

我想要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與多行相關?我怎樣才能解決這個問題?

+0

listview重用可見區域外的項目 – rainash

回答

0

,可能與這樣的事實,在ListView視圖中的Android被回收,所以當你編輯的行可能會看到其中的差別,當你在多行滾動。

一種解決方案可能是更新基礎適配器,然後調用notifyDataSetChanged。但是可能會有更順利的解決方案。

0
  1. 使用View座的設計模式。
  2. 當您從列表視圖中刪除項目時,也從列表中刪除相同項目(在您的個案單元中)。