2013-07-28 58 views
4

我有一个ListView其项目可以通过刷他们删除。当一个项目被刷新时,它的数据库行将被删除,并且它的对象在适配器的数据集中被调用,并且notifyDataSetChanged()也被调用。问题是,这些物品有不同的高度 - 一个可以是一个班轮,第二个可以有四个线。所以,当我有名单上的一些项目,让我们说:ListView项目删除更改项目高度

1.One线

2.两线

3.Three线

4.One线

而且第二个被删除,第三个取代它(如预期的那样),但被切断成两行。当第三个被删除时,第四个项目的高度会增加以匹配已删除的项目。

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

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

电流适配器代码:

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

回答

0

好啦,我已经找到了解决办法。

新增int lastValidPosition = -1; 在适配器,然后删除回调方法adapter.lastValidPosition = position-1adapter.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; 
} 

,它完美的作品。