4

我有一个ListView可以通过滑动删除其项目的项目。当一个项目被滑动时,它的数据库行以及它在适配器数据集中的对象都会被删除,并且notifyDataSetChanged()也会被调用。问题是,这些项目有不同的高度——一个可以是单线,第二个可以有四线。所以当我在清单上有一些项目时,让我们说:

1.一条线

2.两条线

3.三线

4.一条线

第二个被删除,第三个被删除(如预期的那样),但被截断为两行。并且当第三个被删除时,第四个项目的高度会增加以匹配被删除项目的高度。

我找到了解决这个问题的方法,但它可能是错误的——即使 convertView 不为空,它也会创建一个新视图。

我想知道这是否可以通过另一种回收友好的方式来实现。

当前适配器代码:

public class CounterItemAdapter extends BaseAdapter{
    private Activity activity;
    private ArrayList<CounterItem> data;
    private SQLiteOpenHelper helper;
    private static LayoutInflater inflater = null;

    public CounterItemAdapter(Activity activity, ArrayList<CounterItem> data, SQLiteOpenHelper helper) {
        this.activity = activity;
        this.data = data;
        this.helper = helper;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }


    @Override
    public CounterItem getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        //if(convertView == null)
            view = inflater.inflate(R.layout.counter_list_item, null);
        TextView nameView = (TextView)view.findViewById(R.id.nameView);

        //other, unrelated views were here

        final CounterItem counterItem;
        counterItem = getItem(position);

        nameView.setText(counterItem.getName());

        return view;
    }
}
4

1 回答 1

0

好的,我找到了解决方案。

在适配器中添加int lastValidPosition = -1; ,然后adapter.lastValidPosition = position-1 在删除回调方法中,之前adapter.notifyDataSetChanged(); ,以及getView()我更改的适配器的方法中

//if(convertView == null)
    view = inflater.inflate(R.layout.counter_list_item, null);

if(lastValidPosition < position | convertView == null){
    view = inflater.inflate(R.layout.counter_list_item, null);
    lastValidPosition = position;
}

它工作得很好。

于 2013-07-28T17:00:25.793 回答