0

我正在通过两个不同派生类的抽象类访问相同的片段布局,如下所述,基于派生片段,值将在视图上更新。现在我的问题是从静态绑定适配器方法访问覆盖方法,如下所述。处理这种情况的最佳架构方法是什么?

我的 XML 会像 (fragmentLayout.xml)

<layout>
    <data>
        <variable
            name="viewModel"
            type="MyViewModel" />
    </data>
    <ConstraintLayout>
        <Button
             android:id="@+id/button"
             style="@style/buttonStyle"       
             app:name="@{viewModel.buttonName}"/>
    </ConstraintLayout>
</layout>

我的 AbstractFragment 类会像

public abstract class AbstractFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        FragmentLayoutBinding fragmentLayoutBinding = FragmentLayoutBinding.inflate(inflater);
        fragmentLayoutBinding.setLifecycleOwner(this);
        fragmentLayoutBinding.setfragment(this);
        MyViewModel myViewModel = new ViewModelProvider 
                (ContextProvider.getFragmentActivity(),this).get("model", MyViewModel.class);
        fragmentLayoutBinding.setViewModel(myViewModel);
        return fragmentLayoutBinding.getRoot();
    }

    @BindingAdapter(value = {"name"})
    public static void buttonBinding(@NonNull Button button, String name) {
       // update(button, name); => how to access the abstract method from here
    }

    protected abstract void update(Button button, String name);
}

MyDerivedFragments 类会像

protected class MyDerivedFragmentOne extends AbstractFragment {
    @Override
    protected void update(Button button, String name) {
        button.setText(name + "DerviedOne");
    }
}
protected class MyDerivedFragmentTwo extends AbstractFragment {
    @Override
    protected void update(Button button, String name) {
        button.setText(name + "DerviedTwo");
    }
}

请帮帮我!

4

0 回答 0