notifyDataSetChanged() 使列表刷新并滚动跳回顶部 [英] notifyDataSetChanged() makes the list refresh and scroll jumps back to the top

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

问题描述

我正在尝试实现无限滚动列表视图,但是当我调用 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?

推荐答案

这种行为很不正常.在没有看到您的代码的情况下,我可以建议以下操作:

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

1) 您没有从 UI 线程调用 notifyDataSetChanged().正确的做法:

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

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

2) 您是否无意中调用了 adapter.notifyDataSetInvalidated();

2) You accidentally or not are making a call to 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 而不是 list = new 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();

这是我的适配器示例:

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


}

调用适配器:

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();

这样的实现是无穷无尽的,我看到超过 15000 个元素没有任何问题.

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

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

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