Android: TabLayout in another TabLayout (Conflicts)

1

I have a Tablayout that has the fragments of Menu, Entry, Saucer, Dessert and Drink, which have a Recyclerview and Viewpager that is filled with the respective meals, then I put everything in an Activity that has another TabLayout and Viewpager with the purpose of ordering the food for several clients, clients can be added dynamically, now, the problem is that for each client, I use the same fragment, when calling the same fragment, it works, but the Viewpager is not filled with its respective fragments , also, if I slide to the right or left, it does not slide well, it is halfway and moves very slowly.

Any solution?

Here the code:

Activity Detail XML Restaurant

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.eduardoricardez.restominute.RestaurantDetalle">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="150dp">

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



            <ImageView
                android:id="@+id/ivRestaurant"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:layout_alignParentBottom = "true"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/BlackTransparent"
                android:gravity="center"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                android:padding="8dp">


                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_marginStart="10dp"
                    android:gravity="center_vertical">

                    <com.thebrownarrow.customfont.CustomFontTextView
                        android:id="@+id/tvRestaurantName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/cardview_light_background"
                        android:textSize="16sp"
                        android:text="Restaurant"
                        app:custom_typeface="fonts/estandar.ttf"/>
                    <com.thebrownarrow.customfont.CustomFontTextView
                        android:id="@+id/tvRestaurantAddress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/cardview_light_background"
                        android:textSize="14sp"
                        android:text="Adresse"
                        app:custom_typeface="fonts/light.ttf"/>
                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="right"
                    android:layout_marginEnd="10dp">

                    <com.thebrownarrow.customfont.CustomFontTextView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:text="-10%"
                        android:textColor="@color/cardview_light_background"
                        android:textSize="12sp"
                        android:gravity="center"
                        app:custom_typeface="fonts/light.ttf"
                        android:background="@drawable/promotion"
                        android:elevation="2dp"
                        />
                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    <FrameLayout
        android:id="@+id/layout_NavigationTabStrip"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#CB1515"
        android:paddingEnd="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingStart="20dp">
        <android.support.design.widget.TabLayout
            android:id="@+id/ntsClient"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:tabTextColor="#81ffffff"
            app:tabSelectedTextColor="#fff"
            app:tabIndicatorHeight="0dp">

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

        <!--
        <com.gigamole.navigationtabstrip.NavigationTabStrip
            android:id="@+id/ntsMealsClient"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:nts_active_color="#fff"
            app:nts_inactive_color="#81ffffff"
            app:nts_titles="@array/restaurantClient"
            app:nts_typeface="fonts/light.ttf"
            app:nts_weight="0dp"
            app:nts_size="12sp"/>
        -->
    </FrameLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/containerMealsClient"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <com.thebrownarrow.customfont.CustomFontButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ajouter un client"
            android:textColor="@color/cardview_light_background"
            android:background="@drawable/button_precommander"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:custom_typeface="fonts/estandar.ttf"
            android:onClick="agregarCliente"/>

        <com.thebrownarrow.customfont.CustomFontButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Retirer un client"
            android:textColor="@color/cardview_light_background"
            android:background="@drawable/button_precommander"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:custom_typeface="fonts/estandar.ttf"
            android:onClick="retirarCliente"/>
    </LinearLayout>
</LinearLayout>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorPrimary"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

            <ImageView
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:src="@drawable/transparent_banner" />

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:layout_marginEnd="10dp"
                android:background="@drawable/bouton_panier"
                android:onClick="AbrirCarrito"/>
        </RelativeLayout>


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


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

Fragment restaurant_client_fragment

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/layout_NavigationTabStrip"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#CB1515"
    android:paddingEnd="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingStart="20dp">

    <com.gigamole.navigationtabstrip.NavigationTabStrip
        android:id="@+id/ntsMeals"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        app:nts_active_color="#fff"
        app:nts_color="#fff"
        app:nts_factor="2.8"
        app:nts_corners_radius="1.5dp"
        app:nts_inactive_color="#81ffffff"
        app:nts_titles="@array/restaurant"
        app:nts_typeface="fonts/light.ttf"
        app:nts_weight="3dp"
        app:nts_size="12sp"/>

</FrameLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/containerMeals"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

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

row_meals_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"
            android:padding="20dp">

            <com.thebrownarrow.customfont.CustomFontTextView
                android:id="@+id/tvMealTitulo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 1"
                android:textSize="20sp"
                app:custom_typeface="fonts/estandar.ttf"
                />
            <com.thebrownarrow.customfont.CustomFontTextView
                android:id="@+id/tvMealPrecio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="15.50 €"
                android:textSize="14sp"
                app:custom_typeface="fonts/light.ttf"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
            <com.thebrownarrow.customfont.CustomFontTextView
                android:id="@+id/tvMealDescripcion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Description"
                android:textSize="16sp"
                app:custom_typeface="fonts/estandar.ttf"
                android:textAlignment="center"
                />
        </LinearLayout>

    </LinearLayout>




</android.support.v7.widget.CardView>

The java

RestaurantDetalle.java

public class RestaurantDetalle extends AppCompatActivity {


CustomFontTextView tvRestaurantName;
CustomFontTextView tvRestaurantAddress;
ImageView ivRestaurant;

ViewPager mViewPager;
//NavigationTabStrip tabLayout;
TabLayout tabLayout;


//Tabs
private ClientePageAdapter mClientePageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.restaurant_fragment);


    Intent intent = getIntent();


    tvRestaurantName = (CustomFontTextView) findViewById(R.id.tvRestaurantName);
    tvRestaurantAddress = (CustomFontTextView) findViewById(R.id.tvRestaurantAddress);
    ivRestaurant = (ImageView) findViewById(R.id.ivRestaurant);

    Picasso.with(this)
            .load("https://restominute-dev.herokuapp.com/upload/"+intent.getStringExtra("restaurantId")+"?token=FYfZ%2Fo28Mva6MGYnFOdiSOPPdVwSbTuYd86P3wYlq%2Fb5CDqHJwUgQKX5aALSiBP4XQC3TswG8I37%2F4K62ZUnx11n3MIc7sckbDCs8kFbbDMbuql8p0chLLHV20FLu90DfyCSkSvP9D2NAHgNAF8jk76%2B717S2qo%2F7XCw7QQtxIc%3D")
            .resize(350,300)
            .placeholder(R.color.colorAccent)
            .into(ivRestaurant);

    tvRestaurantName.setText(intent.getStringExtra("restaurantName"));
    tvRestaurantAddress.setText(intent.getStringExtra("restaurantAddress"));
    Meal.setRestaurantID(intent.getStringExtra("restaurantId"));

    // TABS Y VIEWPAGER
    mClientePageAdapter = new ClientePageAdapter(getSupportFragmentManager());

    //tabLayout = (NavigationTabStrip) findViewById(R.id.ntsMealsClient);
    mViewPager = (ViewPager) findViewById(R.id.containerMealsClient);
    setupViewPager(mViewPager);

    tabLayout = (TabLayout) findViewById(R.id.ntsClient);
    tabLayout.setupWithViewPager(mViewPager);



}

private void setupViewPager(ViewPager viewpager){

    mClientePageAdapter.addFragment(new TabMealsClientFragment(),"Cliente 1" );
    mClientePageAdapter.addFragment(new TabMealsClientFragment(),"Cliente 2" );
    viewpager.setAdapter(mClientePageAdapter);
}

private void addTab(String title) {
    tabLayout.addTab(tabLayout.newTab().setText(title));
    mClientePageAdapter.addTabPage(title);
}

public void AbrirCarrito(View view){
    Intent PanierIntent = new Intent(this, PanierActivity.class);
    this.startActivity(PanierIntent);
}

}

TabsMealsClientFragment.java

public class TabMealsClientFragment extends Fragment {

ViewPager mViewPager;
NavigationTabStrip tabLayout;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.restaurant_client_fragment, container, false);

// Set up the viewPager with the sections adapter.         mViewPager = (ViewPager) view.findViewById (R.id.containerMeals);

    SectionsPageAdapter adapter = new SectionsPageAdapter(getActivity().getSupportFragmentManager());
    mViewPager.setAdapter(adapter);

    tabLayout = (NavigationTabStrip) view.findViewById(R.id.ntsMeals);
    tabLayout.setViewPager(mViewPager);

    return view;
}

}

Adapters

public class ClientePageAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragmentList= new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public void addTabPage(String title) {
    mFragmentTitleList.add(title);
    notifyDataSetChanged();
}

public void addFragment(Fragment fragment , String title){
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}

public ClientePageAdapter(FragmentManager fm) {
    super(fm);


}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}

}

public class SectionsPageAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragmentList= new ArrayList<>();

public SectionsPageAdapter(FragmentManager fm) {
    super(fm);

    mFragmentList.add(new TabMenuFragment());
    mFragmentList.add(new TabEntreeFragment());
    mFragmentList.add(new TabPlatFragment());
    mFragmentList.add(new TabDessertFragment());
    mFragmentList.add(new TabBoissonFragment());

}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}

}

    
asked by Eduardo Ricardez 20.04.2017 в 17:51
source

1 answer

2

I solved it with getChildFragmentManager in the second Tab

SectionsPageAdapter adapter = new SectionsPageAdapter(getChildFragmentManager());
    
answered by 20.04.2017 / 18:13
source