2015-05-29 117 views
2

項目listView在刪除項目後重疊。爲什麼?例如之前並刪除第一個項目後:ListView項目重疊

public void updateData(List<ParseObject> data) { 
    mGroupList.clear(); 
    mGroupList.addAll(data); 
    notifyDataSetChanged(); 
} 

的ListView:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:divider="@null"/> 

getView適配器:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder viewHolder; 

    if (convertView == null) { 
     viewHolder = new ViewHolder(); 
     convertView = mLayoutInflater.inflate(R.layout.card_item, parent, false); 
     viewHolder.mCardViewNative = (CardViewNative) convertView.findViewById(R.id.card_view); 
     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    //code 

    viewHolder.mCardViewNative.setCard(card); 

    return convertView; 
} 

enter image description here

我使用的方法刪除元素

card_item.xml:

<it.gmariotti.cardslib.library.view.CardViewNative 
android:background="@drawable/header_dark" 
android:id="@+id/card_view" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"/> 

UPDATE

如果不使用ViewHolder和膨脹視圖每次,不存在重疊。但每次膨脹視圖都不好。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    convertView = mLayoutInflater.inflate(R.layout.card_item, parent, false); 
    CardViewNative cardViewNative = (CardViewNative) convertView.findViewById(R.id.card_view); 

    //code 

    cardViewNative.setCard(card); 

    return convertView; 
} 
+0

你可以分享你的代碼靜態類? – Meenal

+0

是否在刪除和添加後調用notifyDataSetChanged()? –

+0

@MeenalSharma [我在github上的代碼](https://github.com/lborlandl/Debts/blob/master/app/src/main/java/ua/ck/geekhub/ivanov/debts/adapters/GroupAdapter.java) – Max

回答

0

嘗試

convertView = mLayoutInflater.inflate(R.layout.card_item, null); 

相反

convertView = mLayoutInflater.inflate(R.layout.card_item, parent, false); 

它爲我,用ViewHolder作爲

+0

我使ViewHolder爲靜態。但不幸的是,它無論如何都不適合我。 – Max