1

enter image description here

I have some sort of architecture issue:

I have Activity in which I need to show one of 3 views (one view at one moment in time):

1. One view with stable AppBarLayout + another part of the screen is NestedScrollView that needs to be in separate fragment.
2. One view with stable AppBarLayout + another part of screen need to be a fragment with two tabs to switch between them.
3. Connection lost view - is a fullscreen view without AppBar - just to show that there is no connection.

The upper part of AppBarLayout - I made. Looks like good, but I need runtime to switch Fragments depending on the response from Server. I am sending a request to the server in onCreate method in MainActivity, and then in onDataLoadedFromServer callback, depending on response - I am deciding which fragment to create. But my application crashes, because fragments fields are not initialized properly - the view is not attached to fragment. When I am adding fragment in onCreate method - everything is working good, but at that time I don't have a response from server.

  1. Where to place TabLayout? And where ViewPager? Do I need to put TabLayout in main_activity.xml, and ViewPager in separate fragment_2_layout. And then add it to FrameLayout (container for fragment which is located in main_activity)?
  2. Is it good practice to load some data from the server in the fragment? Or it's better to load data in Activity and then set it through the method call to Fragment?

Please, provide some sort of pseudo code to understand logic. Thanks!.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/shop_final_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lampa.letyshops.view.activity.ShopFinalActivity">

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout>

            <include
                layout="@layout/collapsing_part" />

            <android.support.v7.widget.Toolbar>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

        </android.support.v7.widget.Toolbar> 
        </android.support.design.widget.CollapsingToolbarLayout>

        <LinearLayout
            android:id="@+id/content_layout_dual_tabs">

            <android.support.design.widget.TabLayout />

            <android.support.v4.view.ViewPager/>

        </LinearLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <include layout="@layout/shop_final_no_connection_layout" />

</android.support.design.widget.CoordinatorLayout>


public MainActivity {

 protected void onCreate(Bundle savedInstanceState) {
  loadDataFromServer();
 }

 private void onDataLoaded(Object dto) {
  if (showOneViewLayout) {
   showOneViewLayout();
  } else {
   showTabViewLayout();
  }
 }
}

private void showOneViewLayout() {
 f1 = Fragment1.newInstance(params);
 showFragmentWithoutBackStack(R.id.fragment_holder, f1);
}

private void showTabViewLayout() {
 f2 = Fragment2.newInstance();
 showFragmentWithoutBackStack(R.id.fragment_holder, f2);
}

protected void showFragmentWithoutBackStack(int containerViewId, Fragment fragment) {
 Fragment previousFragment = currentFragment;
 Fragment currentFragment = fragment;
 String fragmentTag = fragment.getClass().getSimpleName();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 if (previousFragment != null) {
  fragmentTransaction.hide(previousFragment);
 }
 fragmentTransaction.add(containerViewId, fragment, fragmentTag)
  .commitAllowingStateLoss();
}
4

0 回答 0