微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

>确定视图是否符合重用条件的逻辑
>管理可重复使用的视图池的代码
>从池中删除可重复使用的View并重置其属性值以表示逻辑上不同的View的代码

编辑:博客文章Developing applications for Android – gotchas and quirks给出了以下示例:

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!

解决方法:

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

以下是文档摘录:

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐