0

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!

Anushiya
  • 25
  • 9
  • What error you are getting? The code looks fine to me. – OhhhThatVarun Oct 06 '21 at 05:52
  • Thanks for showing interest !! I'm calling "update(button, name)" method inside static binding adapter of Abstract class which is a abstract method. Point 1: Non-Static method (update()) cannot be called inside static method(binding adapter) Point 2: If change (update()) to static , it cannot be overridden in the derived classes . So Here comes the problem! Accessing Abstract method inside Binding Adapter!!! – Anushiya Oct 06 '21 at 06:54

0 Answers0