3

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

  1. 确定 aView是否有资格重复使用的逻辑
  2. View管理可重复使用的 s池的代码
  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将不再指向用户首先单击以打开对话框的元素!

4

2 回答 2

3

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.

于 2011-02-04T05:48:36.843 回答
1

我认为您正在寻找的一个很好的例子是AbsListView.RecycleBin位于widget包中的内部类中。
您可以在这里在线查看代码: 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.

于 2011-02-04T03:26:32.823 回答