Android 中管理 View 重用的代码在哪里?
Android 中管理 View
重用的源代码在哪里?我可以想到此过程的三个不同部分,但可能还有更多:
- 确定
View
是否有资格重用的逻辑 管理 View
池的代码>可以重用的- 代码从池中删除可重用的
View
并重置其属性值以表示逻辑上不同的View
编辑: 博客文章开发Android 应用程序 – 陷阱和怪癖 给出了以下示例:
public class PencilWise extends ListActivity {
View activeElement;
// ...
@Override
public void onCreate ( Bundle savedInstanceState ) {
// ...
this.getListView( ).setOnItemClickListener ( new OnItemClickListener ( ) {
public void onItemClick ( AdapterView<?> parent, View view, int position, long id ) {
MyActivity.this.activeElement = view;
MyActivity.this.showDialog ( DIALOG_ANSWER );
}
} );
}
}
showDialog
方法将显示答案对话框,该对话框需要知道用户打开了什么问题。问题是,当对话框打开时,传递给onItemClick
的视图可能已被重用,因此activeElement
将不再指向用户单击打开的元素首先是对话框!
Where is the source code that manages View
re-use in Android? I can think of three distinct parts to this process, but there may be more:
- The logic that determines if a
View
is eligible for re-use - The code that manages pools of
View
s that can be re-used - The code that removes a re-usable
View
from the pool and resets its property values to represent a logically differentView
EDIT: The blog post Developing applications for Android – gotchas and quirks gives the following example:
public class PencilWise extends ListActivity {
View activeElement;
// ...
@Override
public void onCreate ( Bundle savedInstanceState ) {
// ...
this.getListView( ).setOnItemClickListener ( new OnItemClickListener ( ) {
public void onItemClick ( AdapterView<?> parent, View view, int position, long id ) {
MyActivity.this.activeElement = view;
MyActivity.this.showDialog ( DIALOG_ANSWER );
}
} );
}
}
The
showDialog
method will display the answer dialog, which needs to know what question the user has opened. The problem is that by the time the dialog opens, the view passed toonItemClick
might have been reused, and soactiveElement
would no longer point to the element the user clicked to open the dialog in the first place!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图回收由
AbsListView
及其子类ListView
和GridView
执行。您可以在这里找到这些类的源代码: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget从
ListView
和AbsListView
开始>。Views recycling is performed by
AbsListView
and their subclassesListView
andGridView
. You can find the source code of these classes here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widgetStart with
ListView
andAbsListView
.我认为您正在寻找的一个很好的例子是位于
widget
包中的AbsListView.RecycleBin
内部类。您可以在此处在线查看代码:
https://android.googlesource.com/platform/frameworks/base/+/android-2.2_r1.1/core/java/android/widget/AbsListView.java#3888
以下是文档摘录:
I think a good example of what you're looking for is in the
AbsListView.RecycleBin
inner class located in thewidget
package.You can see the code online here:
https://android.googlesource.com/platform/frameworks/base/+/android-2.2_r1.1/core/java/android/widget/AbsListView.java#3888
Here's an excerpt from the documentation: