当前位置: 代码迷 >> Android >> Android注释自定义视图
  详细解决方案

Android注释自定义视图

热度:84   发布时间:2023-08-04 12:31:37.0

我正在尝试为我的视图实现Android注释。 但我不知道如何正确地做到这一点。 目前我的问题是,视图中的字段始终为NULL。

我认为我对如何将Android Annotations与Views and Adapters结合使用存在一些理解上的问题。 有人可以提示我如何正确执行此操作吗?

在我的片段中,我使用以下适配器:

ItemAdapter

@EBean
public class ItemAdapter extends BaseAdapter {

public ItemAdapter(Context context) {
    this.context = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    GalleryListView view;
    if (convertView == null) {
        view = GalleryListView_.build(context);
    } else {
        view = (GalleryListView) convertView;
    }

    imageUrls = getDocumentListAll();
    DDocuments doc = documentProxy.getElementByDocument(imageUrls.get(position));
    view.init(imageUrls.get(position), Uri.fromFile(new File(doc.getPath())));

    // before I used annotations I did set my Image using this. But now I dont really know how to use this line
    // ImageLoader.getInstance().displayImage(Uri.fromFile(new File(doc.getPath())).toString(), holder.image, options, animateFirstListener);
}
}

GalleryListView

@EViewGroup(R.layout.gallery_list)
public class GalleryListView extends LinearLayout {
    @ViewById
    ImageView   image;

    @ViewById
    TextView    text;

    public void init(String imageText, Uri imageUri) {
        text.setText(imageText);
        image.setImageURI(imageUri);
    }
}

问题在于调用init时未注入视图。 因此,一种解决方案应该是不直接设置视图的文本。 相反,您想要设置一个变量,您知道该变量将在注入视图后读入。

一种方法是利用 。 在进行View注入之后,将调用带有@AfterViews批注的方法。

在我的头顶上看起来像这样:

@EViewGroup(R.layout.gallery_list)
public class GalleryListView extends LinearLayout {
    String mText;
    Uri mUri;

    @ViewById
    ImageView   image;

    @ViewById
    TextView    text;

    public void init(String imageText, Uri imageUri) {
        mText = imageText;
        mUri = imageUri;
    }

    @AfterViews
    void afterViews() {
        text.setText(mText);
        image.setImageURI(mUri);
    }
}

这可能不是最佳解决方案,但应该可以完成工作。 我建议携带 ; 还有其他类似的注释会派上用场。

您从哪里获得背景信息?

尝试更换

    view = GalleryListView_.build(context);

    view = new GalleryListView_(context);

无论如何,对于如此小的视图,我看不到使用Android Annotations的好处,也许如果您拥有多个我会理解的资源,但是对于如此小的代码,我建议您实现te构造函数并在那里自己膨胀资源。

  相关解决方案