RoboBinding / RoboBinding

A data-binding Presentation Model(MVVM) framework for the Android platform.
http://robobinding.org
Other
1.28k stars 221 forks source link

Fragment support #148

Closed weicheng113 closed 9 years ago

weicheng113 commented 9 years ago

Fragment, ListFragment support.

weicheng113 commented 9 years ago

Fragment is naturally supported from the current version 0.8.5. The demo for Fragment is added to Gallery project.

bj0 commented 9 years ago

The gallery app shows using the more advanced FragmentManager method, but is using simple tags supported?

For instance I have a landscape layout xml that has:

<LinearLayout ...>
    <fragment
        ...
        class="com.example.MainActivity$LeftFragment/>

    <fragment
        ...
        class="com.example.MainActivity$RightFragment/>
</LinearLayout>

And in the Fragment's onCreateView function I have:

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

    PresModel pm = new PresModel();
    BinderFactory factory = new BinderFactoryBuilder()
            .add(new DynamicViewBinding()
                    .forView(Switch.class)
                    .oneWayProperties("checked")
                    .oneWayProperties("enabled"))
            .build();
    View rootView = factory.createViewBinder(inflater.getContext())
            .inflateAndBindWithoutAttachingToRoot(R.layout.fragment_left, PresModel,container);
    return rootView;
}

The .inflateAndBindWithoutAttachingToRoot function throws java.lang.NullPointerException: Root must not be null. This function works fine in my portrait mode layout (it uses a ViewPager).

weicheng113 commented 9 years ago

@bj0,

The container of ViewGroup is null. You will need to use viewBinder.inflateAndBind instead. You can find the example here - https://github.com/RoboBinding/Android-CleanArchitecture/blob/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java

weicheng113 commented 9 years ago

Simple fragment tag is not supported at the moment. We will add the support in future. There is a related issue here - #194 .

bj0 commented 9 years ago

Actually I did get it to work with .inflateAndBind. I put the following in onCreateView

if( container == null )
    rootView = factory.createViewBinder(inflater.getContext())
            .inflateAndBind(R.layout.fragment_left, PresModel);
else
    rootView = factory.createViewBinder(inflater.getContext())
            .inflateAndBindWithoutAttachingToRoot(R.layout.fragment_left, PresModel,container);

And it appears to be working fine now.