Skip to content

组件化项目中R文件无法使用

Drowning Coder edited this page Nov 14, 2018 · 1 revision

熟悉组件化的应该都遇到过R文件中的变量不是常量的问题,著名的butterknife的解决方式是在编译的时候copy一份生成一个R2文件来解决。这里说一下ComponentType注解的解决方式。

@ComponentType(
        value = ComponetId.BANNER,
        view = FrameLayout.class
)
public class BannerVH extends Component<Banner> {
    FrameLayout fgContainer;
    public BannerVH(Context context, View itemView) {
        super(context, itemView);
        fgContainer = (FrameLayout) itemView;
        //再利用LayoutInflater
        LayoutInflater.from(context).inflate()
    }

    @Override
    public void onBind(int pos, Banner item) {

    }
}

可以看到ComponentType提供的有view类型的注解,注解的View类型,就是ViewHolder的itemView,这样就避免了注解中使用R文件的问题。

Clone this wiki locally