栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

notifyDataSetChanged()使列表刷新,并且滚动跳回到顶部

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

notifyDataSetChanged()使列表刷新,并且滚动跳回到顶部

这种行为是不正常的。我看不到您的代码,我建议您执行以下操作:

1)您不是

notifyDataSetChanged()
从UI线程调用。正确的方法:

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

2)您是否偶然拨打了电话

adapter.notifyDataSetInvalidated();

3)在您的适配器中,您重写了

adapter.notifyDataSetChanged();
方法并添加了说明以转到顶部

4)如果您使用列表来填充适配器-
每次都提供新列表,那么适配器设置将被刷新。您应该始终提供相同的列表。但是,您可以根据需要进行任意更改。如果您要重置列表,请使用

list.clear
代替
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.add(someElement);ma.notifyDataSetChanged();

如果需要删除所有项目:

myList.clear();ma.notifyDataSetChanged();

这样的实现是无止境的,我看到了超过一万五千个元素,没有任何问题。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号