Android 中管理 View 重用的代码在哪里?

发布于 2024-10-15 03:43:23 字数 1155 浏览 1 评论 0原文

Android 中管理 View 重用的源代码在哪里?我可以想到此过程的三个不同部分,但可能还有更多:

  1. 确定 View 是否有资格重用的逻辑 管理
  2. View 池的代码>可以重用的
  3. 代码从池中删除可重用的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:

  1. The logic that determines if a View is eligible for re-use
  2. The code that manages pools of Views that can be re-used
  3. The code that removes a re-usable View from the pool and resets its property values to represent a logically different View

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 to onItemClick might have been reused, and so activeElement would no longer point to the element the user clicked to open the dialog in the first place!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-10-22 03:43:23

视图回收由 AbsListView 及其子类 ListViewGridView 执行。您可以在这里找到这些类的源代码: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget

ListViewAbsListView 开始>。

Views recycling is performed by AbsListView and their subclasses ListView and GridView. You can find the source code of these classes here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget

Start with ListView and AbsListView.

花伊自在美 2024-10-22 03:43:23

我认为您正在寻找的一个很好的例子是位于 widget 包中的 AbsListView.RecycleBin 内部类。
您可以在此处在线查看代码:
https://android.googlesource.com/platform/frameworks/base/+/android-2.2_r1.1/core/java/android/widget/AbsListView.java#3888

以下是文档摘录:

RecycleBin 有助于跨布局重用视图。 RecycleBin 有两个级别
存储:ActiveView 和 ScrapView。 ActiveViews 是那些在屏幕上出现的视图
布局的开始。通过构造,它们显示当前信息。结束时
布局中,ActiveView 中的所有视图都会降级为 ScrapView。 ScrapViews 是旧视图
适配器可能会使用它来避免不必要的视图分配。

I think a good example of what you're looking for is in the AbsListView.RecycleBin inner class located in the widget 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:

The RecycleBin facilitates reuse of views across layouts. The RecycleBin has two levels of
storage: ActiveViews and ScrapViews. ActiveViews are those views which were onscreen at the
start of a layout. By construction, they are displaying current information. At the end of
layout, all views in ActiveViews are demoted to ScrapViews. ScrapViews are old views that
could potentially be used by the adapter to avoid allocating views unnecessarily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文