通过Id(数字)与单独的、唯一命名的布局文件区分多个视图实例



我目前正在通过从单个布局文件RecordNoteBox.axml 中膨胀来实例化多个视图实例

RecordNoteBox.axml

<?xml version="1.0" encoding="utf-8"?>
<app.droid.views.custom.PagedFragmentRecordNoteBox
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rb_record_note_box"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#FF555555">

由于我没有专门将每个实例的Id(数字)设置为唯一的非零值,它们都引用了同一个实例,因此通过一个引用更改内容会导致它们显示在视图的所有其他"屏幕副本"中(因为没有更好的术语)。

出于性能原因,哪种方法是首选

(A) 将每个实例的.Id值设置为唯一值(整数)

static int _idCount = 1;
View box1 = _inflater.Inflate(Resource.Layout.RecordNoteBox, null);
View box2 = _inflater.Inflate(Resource.Layout.RecordNoteBox, null);
box1 = _idCount++;
box2 = _idCount++;

(B) 制作布局文件的多个副本,为每个副本指定一个特定的名称

View box1 = _inflater.Inflate(Resource.Layout.RecordNoteBox1, null);
View box2 = _inflater.Inflate(Resource.Layout.RecordNoteBox2, null);

或者这真的重要吗?谢谢

Id只有在使用findViewById()时才重要,因为您已经有了对各个视图的引用,所以这里不需要这样做。我不会麻烦做A或B。

最新更新