编程语言
首页 > 编程语言> > java-在Android中管理View重用的代码在哪里?

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.

标签:android-ui,views,java,android
来源: https://codeday.me/bug/20191105/1995340.html