How to override view binding adapter methods in derived classes in android?

294 views Asked by At

I am accessing the same fragment layout through the abstract class for two different derived class as mentioned below, Based on the derived fragment the value will get updated on the views. Now My problem is to access override method from static binding adapter method as mentioned below. What is the best architectural approach to handle this situation?

My XML will be like (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>

My AbstractFragment class will be like

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);
}

And MyDerivedFragments Classes will be like

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");
    }
}

Kindly help me out!

0

There are 0 answers