notifyDataSetChanged()能够上榜,刷新和滚动跳回到顶端 [英] notifyDataSetChanged() makes the list refresh and scroll jumps back to the top

查看:634
本文介绍了notifyDataSetChanged()能够上榜,刷新和滚动跳回到顶端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现无限滚动的ListView,但是当我打电话 notifyDataSetChanged()整个列表刷新则滚动位置回到顶端。

I am trying to implement an endless scroll listview but when I call notifyDataSetChanged() the whole list refreshes then the scroll position goes back to the top.

这是正常的行为呢?我怎么可以把它简单地添加无刷新添加的项目,并保持滚动位置?

Is this the normal behavior? how can I make it simply add the items added without refreshing and keep the scroll position?

推荐答案

这样的行为是不正常的。没有看到你的code我可以建议如下:

Such behaviour is not normal. Without seeing your code I can suggest following:

1)你是不是叫 notifyDataSetChanged()从UI线程。正确的方法:

1) You are not calling notifyDataSetChanged() from the UI thread. The correct way:

runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

2)你不小心或不正在调用 adapter.notifyDataSetInvalidated();

3)在您的适配器,你重写 adapter.notifyDataSetChanged(); 方法和添加指令,返回页首

3) In your adapter you override the adapter.notifyDataSetChanged(); method and added instruction to go to top

4)如果你使用一个列表来填充适配器 - 你提供新的列表每一次,所以适配器设置被刷新。你应该总是提供相同的列表。然而,你可以尽可能多的,你想改变它。如果你重新设置列表使用 list.clear ,而不是名单=新的ArrayList();

4) If you're using a list to populate adapter - you supply new list every time, so adapter settings are refreshed. You should always supply the same list. However you can change it as much as you want. If you're resetting the list use list.clear instead of list = new ArrayList();

下面是我的适配器的例子:

Here is an example of my adapter:

public class Adapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public MediaItemAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.item_composer, null);
        }
        TextView title = (TextView) vi.findViewById(R.id.item_title); // title
        TextView price = (TextView) vi.findViewById(R.id.price);


        return vi;
    }


}

来电转接器:

Call for adapter:

List myList = new ArrayList<HashMap<String, String>>();
Adapter ma = new Adapter(this, myList);

myList上可以适配器初始化之前是空的。

myList can be empty before adapter initialization.

然后做我的列表中的某些操作:

Then do some operation with my list:

myList.add(someElement);
ma.notifyDataSetChanged();

如果你需要删除所有项目:

if you need delete all items:

myList.clear;
ma.notifyDataSetChanged();

这种实现是pretty的无止境的,我看到更多的则15000元,没有任何问题。

Such implementation is pretty endless, I saw more then 15 thousand elements without any problems.

这篇关于notifyDataSetChanged()能够上榜,刷新和滚动跳回到顶端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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