Android 在 CursorAdapter 中 bindView() 和 newView() 做什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12672749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:24:22  来源:igfitidea点击:

What bindView() and newView() do in CursorAdapter

androidandroid-cursoradapter

提问by Jainendra

I have a custom CursorAdaptorin my project with overridden methods bindView(View view, Context context, Cursor cursor)and newView(View view, Context context, Cursor cursor). I want to know for what CursorAdapterare used for and what is the difference between and uses of overriding bindView()and newView().

CursorAdaptor在我的项目中有一个自定义的覆盖方法bindView(View view, Context context, Cursor cursor)newView(View view, Context context, Cursor cursor). 我想知道用于什么CursorAdapter以及覆盖bindView()newView().

I have read the Developer Siteand thistutorial but still I didn't understood. As I'm a beginner to Android, please help me understand this concept.

我已经阅读了开发人员网站教程,但仍然没有理解。由于我是 Android 的初学者,请帮助我理解这个概念。

回答by Vinay S Shenoy

In order to understand this, you must first understand how BaseAdapterworks, since CursorAdapteris a subclass of BaseAdapter.

为了理解这一点,您必须首先了解它的BaseAdapter工作原理,因为CursorAdapterBaseAdapter.

Android maintains a pool of views for a ListViewwhich it will give to you so you can reuse it instead of creating a new view each time.

Android 维护一个视图池,ListView它将提供给您,以便您可以重复使用它,而不是每次都创建一个新视图。

In BaseAdapter, you will have a function called getView(), to which one of the parameters is a Viewobject named convertView. Basically, this convertViewwill be nullif the list is being loaded for the first time, and it will not be nullonce you start sliding the list. Therefore, in the getView()method of your BaseAdapter, you will check if convertViewis null. If yes, you will inflate it. Then you can use the view and set its elements as normal. This will improve the scrolling performance of a listview tremendously.

在 中BaseAdapter,您将调用一个函数getView(),其中一个参数是一个View名为的对象convertView。基本上,这convertView将是null第一次加载列表时,null一旦开始滑动列表就不会。因此,在getView()您的方法中BaseAdapter,您将检查是否convertViewnull。如果是,你会膨胀它。然后您可以使用该视图并将其元素设置为正常。这将极大地提高列表视图的滚动性能。

A CursorAdaptermakes it easy to use when the data source of a listview is a database. In a cursor adapter, however, Android takes care of checking whether the convertViewis nullor not. In the newView()method, you simply inflate the view and return it. In the bindView()method, you set the elements of your view.

CursorAdapter当列表视图的数据源是数据库时,A很容易使用。在光标适配器,不过,Android负责检查的是否convertViewnull或不是。在该newView()方法中,您只需膨胀视图并返回它。在该bindView()方法中,您设置视图的元素。

As an example, imagine a listview on a device which can show upto 11 list items on the screen. In this case, newView()will be called upto 11 times. However, bindView()will be called many times whenever you scroll the list view. The 11 views you created in your newView method will be reused again and again as you scroll the list.

例如,假设设备上的列表视图最多可以在屏幕上显示 11 个列表项。在这种情况下,newView()最多将被调用 11 次。但是,bindView()每当您滚动列表视图时,都会被多次调用。您在 newView 方法中创建的 11 个视图将在您滚动列表时一次又一次地重复使用。