Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

Android - Abstract fragment with implemented methods and unknown Databinding class

I would like to implement a fragment heritage but I don't know how to deal with ViewDataBinding generated classes.

Example :

fragment_a.xml

<layout ...>
    <data>
        <variable
            name="viewModel"
            type="...myViewModelA" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout...>

        <include
            android:id="@+id/form_section"
            layout="@layout/form"
            app:myData="@{myViewModelA.textValue}"
            .../>

        <MyWidget1
            android:id="@+id/widget1"
            .../>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

fragment_b.xml

<layout ...>
    <data>
        <variable
            name="viewModel"
            type="...myViewModelB" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout...>

        <include
            android:id="@+id/form_section"
            layout="@layout/form"
            app:myData="@{myViewModelB.textValue}"
            .../>

        <MyWidget2
            android:id="@+id/widget2"
            .../>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

The only differences are the viewModel used and the widgets.
Their viewModel are herited from a base ViewModel.
Both fragment use a "form" layout via the include. Let's say we have the "form" layout like this :

<layout ...>
    <data>
        <variable
            name="myData"
            type="String" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout...>

        <TextView
            android:id="@id/textview"
            android:text="@{myData}

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

So I would like to create an abstract BaseFragment that could implement methods manipulating attributes from the "form" layout, pretty much like this :

public abstract class BaseFragment<V extends BaseViewModel, B extends ViewDataBinding> extends Fragment {
    protected V viewModel;
    protected B binding;
    // I skip the affectation code of the viewModel and binding objects using the generics
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ...

        myMethod();

        return v;
    }

    protected void myMethod() {
        // some action on binding.formSection
    }
}

As I said, the goal is to centralize the code manipulating attributes from the "form" layout in the abstract class and implement the code manipulating attributes from the specific fragment in sub fragment extending that abstract BaseFragment.

Here is the thing, as you can imagine I can't access attributes from the binding objet through the abstract fragment (the binding object is just a ViewDataBinding object to the abstract class).

I kind of need to have an interface defining attributes that are available in the two generated classes FragmentABinding and FragmentBBinding which are common concerning the "form" layout.

Is there a way to adresse this kind of need ? A pattern I'm not aware of ? Something I missed ?

question from:https://stackoverflow.com/questions/65833403/android-abstract-fragment-with-implemented-methods-and-unknown-databinding-cla

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...