ListView项目重叠

时间:2015-05-29 13:03:24

标签: android listview android-listview listviewitem

项目listView在删除项目后重叠。为什么?例如,在删除第一项之前和之后:

enter image description here

我正在使用方法删除元素:

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;
}

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"/>

更新

如果不是每次都使用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;
}

1 个答案:

答案 0 :(得分:0)

尝试

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

而不是

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

它适用于我,使用ViewHolder作为静态类