安卓.notifyDataSetChanged() 方法和 ListViews 是如何工作的? [英] Android. How does notifyDataSetChanged() method and ListViews work?

查看:29
本文介绍了安卓.notifyDataSetChanged() 方法和 ListViews 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 ListView 概念及其工作原理,并且我正在尝试创建我自己的扩展 BaseAdapter 的适配器.例如,对于 ArrayAdapter,有一个 notifyDataSetChanged() 方法应该在您更新保存所有数据的数组列表后调用,以刷新 notifyDataSetChanged()代码>列表视图.

I am trying to understand the ListView concept and how it works and I'm trying to create my own adapter which extends BaseAdapter. For ArrayAdapter for instance, there is the notifyDataSetChanged() method which should be called after you've updated the array list which holds all your data, in order to refresh the ListView.

但我正在创建自己的 BaseAdapter 子类.那个方法对我不可用,是吗?我如何实现这个方法?基本上,那个方法到底做了什么,也许我会明白.

But I am creating my own subclass of BaseAdapter. That method is not available to me, or is it? How do i implement this method? Basically, what does that method do exactly, maybe I'll understand then.

ArrayAdapter 的情况下,我猜它会查看 ListView 当前显示的位置,并检查它是否与 中的位置相同ArrayList 更新后?或者...

In case of the ArrayAdapter i'm guessing it looks at what position the ListView is currently displaying and it checks if it's the same one as in the ArrayList after it was updated? Or...

它说的方法:

通知附加的观察者底层数据已经被已更改,任何反映数据集的视图都应自行刷新.

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

但它究竟是如何自我刷新的?

But how exactly does it refresh itself?

谁能解释一下?

推荐答案

我想通了.我无法理解适配器到底是如何启动的,以及它是如何知道从哪里获取数据的.当我扩展 BaseAdapter 类时,在该类的构造函数中,我初始化了我想在 ListView 中看到的项目列表.但我无法弄清楚这些值将如何使用以及何时使用.

I've figured it out. I couldn't understand how the hell the adapter started and how did it know where to get the data from. When i extended the BaseAdapter class, in the constructor of that class I initialized the list of items that I wanted to see in the ListView. But I couldn't figure out how these values would be used and when.

事情就是这样!!!:

BaseAdapter 中有一些方法需要重写.其中,有getCount().

In the BaseAdapter there are some methods that need to be overridden. Among these, there is getCount().

ListView 被创建时,它会调用 getCount().如果这返回一个不为 0 的值(我返回了我之前在构造函数中初始化的 ArrayList 的大小),那么它会调用 getView() 足够多的时间来用项目填充屏幕.例如,我用 20 个项目初始化了 ArrayList.因为最初只有 8 个项目适合屏幕,所以 getView() 被调用了 8 次,每次都要求我返回所需的位置(更准确地说,它想知道该行的样子在该特定位置的列表中,它需要包含哪些数据).如果我向下滚动列表,getView() 会一遍又一遍地被调用,直到我到达列表的末尾,在我的例子中是 20 个项目/行.

When the ListView is created and whatnot, it calls getCount(). If this returns a value different than 0 (I returned the size of the ArrayList which I've previously initialized in the constructor), then it calls getView() enough times to fill the screen with items. For instance, I initialized the ArrayList with 20 items. Because only 8 items initially fit on the screen, getView() was called 8 times, each time asking for the position it required for me to return (more precisely it wanted to know how the row would look like in the list on that specific position, what data it needed to contain). If I scroll down the list, getView() gets called over and over again, 'til I hit the end of the list, in my case 20 items / rows.

notifyDataSetChanged() 的作用是......调用时,它会查看调用时屏幕上显示的项目(更准确地说是哪些行索引)并调用 getView() 与这些位置.

What notifyDataSetChanged() does is ... when called, it looks at what items are displayed on the screen at the moment of its call (more precisely which row indexes ) and calls getView() with those positions.

ie 如果您要显示列表中的前 8 个项目(因此这些是屏幕上可见的项目)并且您在列表中的第 2 个和第 3 个项目之间添加另一个项目,并且您调用 notifyDataSetChanged() 然后 getView() 被调用 8 次,位置从 0 开始到 7 结束,因为在 getView()> 方法,您从 ArrayList 获取数据,然后它会自动返回插入列表中的新项目与前 8 个中的 7 个(7 而不是 8,因为最后一个项目向下一个位置,所以它不再可见),并且 ListView 将使用这些项目重绘或其他任何内容.

i.e. if you're displaying the first 8 items in the list (so those are the ones visible on the screen) and you add another item between the 2nd and 3rd item in the list and you call notifyDataSetChanged() then getView() is called 8 times, with positions starting from 0 and ending with 7, and because in the getView() method you're getting data from the ArrayList then it will automatically return the new item inserted in the list alongside 7 out of the previous 8 (7 and not 8 because the last item went one position down, so it is not visible anymore), and the ListView will redraw, or whatever, with these items.

另外,重要的是要指定的是,如果您正确实现了 getView(),您最终将回收已显示的项目(对象)(而不是创建新项目).在附近观看此视频12:00分看getView()

Also, important to specify is that if you've implemented getView() correctly, you'll end up recycling the items (the objects) already displayed (instead of creating new ones). See this video at around 12:00 minutes to see the correct way to implement getView()

通过在每个方法中调用 LogCat 并跟踪正在发生的事情,我已经弄清楚了这一切.

I've figured all this out by placing calls to LogCat in every method and following what was going on.

希望这对刚刚开始了解 ListView 的工作原理的人有所帮助.

Hope this helps someone who's just now starting to understand how ListViews work.

附言这个例子也帮助了我很多东西要理解.

P.S. This example also helped me a lot to understand.

更新

现在 ListViews 已经不再使用了.Android 推出了 RecyclerView,它为您回收视图,但了解 ListView 的基础知识有助于理解 RecyclerView.

Nowadays ListViews are not really used anymore. Android came out with the RecyclerView which does the recycling of the views for you, but knowing the basics of a ListView helps with understanding the RecyclerView.

这里有一个链接供参考:https://developer.android.com/指南/主题/ui/layout/recyclerview

Here's a link for reference: https://developer.android.com/guide/topics/ui/layout/recyclerview

这篇关于安卓.notifyDataSetChanged() 方法和 ListViews 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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