ListView项删除更改项目的高度 [英] ListView item deletion changes item heights

查看:157
本文介绍了ListView项删除更改项目的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,其项目可以通过滑动他们被删除。当一个项目被刷,其数据库行被删除,以及其在适配器的数据集对象,和 notifyDataSetChanged()被称为为好。问题是,这些项目有不同的高度 - 一个人可以是一个班轮,第二可以有四根线。所以当我有名单上的一些项目,让我们说:

1.One行

2.两行

3.Three行

4.One行

和第二个被删除,第三个在它的地方去(如预期),但被切断两行。当第三个被删除时,第四个项目的高度增加以匹配已删除的项目的

我找到一种方法来解决这个问题,但它可能是一个错误的 - 它创建了一个新的观点即使convertView不为空

我不知道这是否可以在另一个实现,回收友好的方式。

当前适配器code:

 公共类CounterItemAdapter延伸BaseAdapter {
    私人活动活动;
    私人的ArrayList< CounterItem>数据;
    私人SQLiteOpenHelper帮手;
    私有静态LayoutInflater吹气= NULL;    公共CounterItemAdapter(活动活动,ArrayList的< CounterItem>的数据,SQLiteOpenHelper助手){
        this.activity =活动;
        this.data =数据;
        this.helper =帮手;
        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }    @覆盖
    公众诠释的getCount(){
        返回data.size();
    }
    @覆盖
    公共CounterItem的getItem(INT位置){
        返回data.get(位置);
    }
    @覆盖
    众长getItemId(INT位置){
        返回的getItem(位置).getId();
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看查看= convertView;
        //如果(convertView == NULL)
            鉴于= inflater.inflate(R.layout.counter_list_item,NULL);
        TextView的nameView =(TextView中)view.findViewById(R.id.nameView);        //其他不相关的意见在这里        最后CounterItem counterItem;
        counterItem =的getItem(位置);        nameView.setText(counterItem.getName());        返回视图。
    }
}


解决方案

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

新增 INT lastValidPosition = -1;
在适配器,那么<​​code> adapter.lastValidPosition =位置1
在删除回调方法之前, adapter.notifyDataSetChanged();
,并在适配器的 getView()方法我已经改变了

  //如果(convertView == NULL)
    鉴于= inflater.inflate(R.layout.counter_list_item,NULL);

 如果(lastValidPosition&LT;仓位| convertView == NULL){
    鉴于= inflater.inflate(R.layout.counter_list_item,NULL);
    lastValidPosition =位置;
}

和它完美的作品。

I have a ListView whose items can be deleted by swiping them. When an item is swiped, its database row gets deleted, as well as its object in the adapter's data set, and notifyDataSetChanged() is called as well. The problem is, these items have varying heights - one can be a one-liner, the second can have four lines. So when I have some items on the list, let's say:

1.One line

2.Two lines

3.Three lines

4.One line

And the second one is deleted, the third one goes in its place (as expected), but gets cut off to two lines. And when the third one is deleted, the fourth item's height is increased to match the deleted item's.

I found a way to fix that, however it's probably a wrong one - it creates a new view even if a convertView is not null.

I wonder if this can be achieved in another, recycling-friendly way.

Current adapter code:

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

解决方案

Okay, i've found a solution.

Added int lastValidPosition = -1; in the adapter, then adapter.lastValidPosition = position-1 in the deletion callback method, before adapter.notifyDataSetChanged(); , and in the getView() method of the adapter i've changed

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

to

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

and it works perfectly.

这篇关于ListView项删除更改项目的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆