是否可以制作2个或更多的DataBindingComponent?

时间:2018-09-21 11:07:24

标签: android android-databinding non-static

我想问问是否可以在Android中制作2个或更多的DataBindingComponent类?因为我想在绑定中转义静态方法,所以我尝试将注入与DataBindingComponent一起使用,但是我得到了错误,必须声明类'AppDataBindingComponent'为抽象或在'DataBindingComponent'中实现抽象方法'getLoginViewDataBinding'< / strong>,由于此错误,我无法制作非静态的错误。

这是我遇到问题的班级

public class AppDataBindingComponent implements android.databinding.DataBindingComponent {
    @Override
    public RecyclerViewDataBinding getRecyclerViewDataBinding() {
        return new RecyclerViewDataBinding();
    }
}

第一个绑定类

public class RecyclerViewDataBinding {

    @BindingAdapter({"app:adapter", "app:data"})
    public void bind(RecyclerView recyclerView, DataAdapter adapter, List<DataModel> data) {
        recyclerView.setAdapter(adapter);
        adapter.updateData(data);
    }
}

第二绑定类

public class LoginViewDataBinding {

    @BindingAdapter({"validation", "errorMsg"})
    public void setErrorEnable(TextInputLayout textInputLayout, StringRule stringRule,
                                      final String errorMsg) {
        Observable<CharSequence> textObservable = RxTextView.textChanges(
                Objects.requireNonNull(textInputLayout.getEditText()));
        compositeDisposable.add(textObservable
                .map(charSequence -> {
                    ......
                })
                .distinctUntilChanged()
                .replay(1).refCount()
                .subscribe());
    }

}

在主类中,我称为DataBindingComponent

public class MainActivity extends AppCompatActivity {
    private DataViewModel dataViewModel;
    private ActivityMainListMvvmBinding activityBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind();
    }

    private View bind() {
        activityBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_main_list_mvvm, new AppDataBindingComponent());
        dataViewModel = new DataViewModel();
        activityBinding.setViewModel(dataViewModel);
        return activityBinding.getRoot();
    }

}

如果我放入 getLoginViewDataBinding

,问题就解决了
public class AppDataBindingComponent implements android.databinding.DataBindingComponent {
    @Override
    public RecyclerViewDataBinding getRecyclerViewDataBinding() {
        return new RecyclerViewDataBinding();
    }

    @Override
    public LoginViewDataBinding getLoginViewDataBinding() {
        return null;
    }
}

我想要的答案是这样的:(这可能吗?)

public class AppDataBindingComponent implements android.databinding.DataBindingComponent {
    @Override
    public RecyclerViewDataBinding getRecyclerViewDataBinding() {
        return new RecyclerViewDataBinding();
    }
}

public class LoginDataBindingComponent implements android.databinding.DataBindingComponent {
    @Override
    public LoginViewDataBinding getLoginViewDataBinding() {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

那呢:

public class DataBindingComponent<T> implements android.databinding.DataBindingComponent {
    private T activity;

    public DataBindingComponent(T activity) {
        this.activity = activity;
    }

    public LoginViewDataBinding getLoginViewDataBinding() {
        return (LoginViewDataBinding) activity;
    }

    public RecyclerViewDataBinding getRecyclerViewDataBinding() {
        return (RecyclerViewDataBinding) activity;
    }
}

而不是在两个课程中都创建:

new AppDataBindingComponent(this)