Reuse of same layouts inside one layout using multiple includes - TabLayout + ViewPager + RecyclerView (multiple viewpagers in one View)

Kohls

I am using a Galaxy Tab E SM-T560. I am a 3 years android developer, just working on internet-search-problem-solving. I have searched for View implementations like inflation and getParent() things, and none of them worked.

In my case I have a layout for tablets, that has 2 includes of a Layout that has TabLayout plus ViewPager, and with this viewpager I use it with recyclerview

My code is not working at some point, I have made some copy paste to know why is not working, but didn't proceed for long searches.

Here is an image of only one working view inside this

http://i.stack.imgur.com/UZigU.png

Here is the code of the fragment that does all of this magic in the right of the drawer:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import br.com.adriankohls.volkmannbus2.R;
import br.com.adriankohls.volkmannbus2.fragmentstatepageadapters.horario.HorarioBlumenauPomerodeFragmentStatePageAdapter;
import br.com.adriankohls.volkmannbus2.fragmentstatepageadapters.horario.HorarioPomerodeBlumenauFragmentStatePageAdapter;
import br.com.adriankohls.volkmannbus2.utils.AppUtils;

public class HorarioFragment extends Fragment {

private boolean isToBlumenau;
private View rootView;

public static HorarioFragment newInstance(boolean isToBlumenau) {
    Bundle bundle = new Bundle();
    HorarioFragment fragment = new HorarioFragment();
    fragment.isToBlumenau = isToBlumenau;
    bundle.putString(fragment.getTag(), AppUtils.getFragmentKey(fragment));
    fragment.setArguments(bundle);
    return fragment;
}

public HorarioFragment() {
    super();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (rootView == null) {
        if (!AppUtils.isTablet()) {
            rootView = inflater.inflate(R.layout.fragment_viewpager_tablayout_horario, container, false);
            ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
            TextView tvDestino = (TextView) rootView.findViewById(R.id.tvDestino);
            ImageView imgInfo = (ImageView) rootView.findViewById(R.id.imgInfo);
            if (isToBlumenau) {
                viewPager.setAdapter(new HorarioPomerodeBlumenauFragmentStatePageAdapter(getFragmentManager(), getContext()));
                tvDestino.setText(getResources().getStringArray(R.array.rota_horario)[0]);
            } else {
                viewPager.setAdapter(new HorarioBlumenauPomerodeFragmentStatePageAdapter(getFragmentManager(), getContext()));
                tvDestino.setText(getResources().getStringArray(R.array.rota_horario)[1]);
            }

            TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            AppUtils.tabLayoutRuleTablet(tabLayout);

            tvDestino.setOnClickListener(switchHorario());
            imgInfo.setOnClickListener(infoHorario());
        } else {
            rootView = inflater.inflate(R.layout.horario_tablet, container, false);

            rootView.post(new Runnable() {
                @Override
                public void run() {
                    setUpPomerodeBlumenau();
                    setUpBlumenauPomerode();
                }
            });
        }
    }
    return rootView;
}

private void setUpPomerodeBlumenau() {
    View view = rootView.findViewById(R.id.layout_horario_pombnu);

    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(
            new HorarioPomerodeBlumenauFragmentStatePageAdapter(
                    getChildFragmentManager(),
                    getContext()));

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setUpBlumenauPomerode() {
    View view = rootView.findViewById(R.id.layout_horario_bnupom);

    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(
            new HorarioBlumenauPomerodeFragmentStatePageAdapter(
                    getChildFragmentManager(),
                    getContext()));

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

This is my layout horario_tablet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/layout_horario_pombnu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:paddingBottom="4dp"
            android:paddingTop="10dp"
            android:text="@string/pomerodeblumenau"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />

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

    </LinearLayout>

    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#FFF" />

    <LinearLayout
        android:id="@+id/layout_horario_bnupom"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:paddingBottom="4dp"
            android:paddingTop="10dp"
            android:text="@string/blumenaupomerode"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />

        <include layout="@layout/fragment_viewpager_tablayout" />
    </LinearLayout>

</LinearLayout>

And this the fragment_viewpager_tablayout.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            app:tabBackground="@color/colorPrimaryDark"
            app:tabGravity="center"
            app:tabIndicatorColor="@android:color/white"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@color/colorPrimary" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

RecyclerView is working fine, for phones all of the views are fine, just on this case, the second layout inside the horario_tablet.xml show the tabs but won't show the fragments for each tab on tablayout of the second layout (@+id/layout_horario_bnupom), even the code is the same.

Kohls

Found the answer I have replaced all the tags for the hardcoded XML inclusion before in horario_tablet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/layout_horario_pombnu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:paddingBottom="4dp"
            android:paddingTop="10dp"
            android:text="@string/pomerodeblumenau"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_pombnu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs_pombnu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimaryDark"
                    app:tabBackground="@color/colorPrimaryDark"
                    app:tabGravity="center"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabMode="scrollable"
                    app:tabSelectedTextColor="@android:color/white"
                    app:tabTextColor="@color/colorPrimary" />
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager_pombnu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/appbar"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>

    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#FFF" />

    <LinearLayout
        android:id="@+id/layout_horario_bnupom"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:paddingBottom="4dp"
            android:paddingTop="10dp"
            android:text="@string/blumenaupomerode"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_bnupom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs_bnupom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimaryDark"
                    app:tabBackground="@color/colorPrimaryDark"
                    app:tabGravity="center"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabMode="scrollable"
                    app:tabSelectedTextColor="@android:color/white"
                    app:tabTextColor="@color/colorPrimary" />
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager_bnupom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/appbar"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>
</LinearLayout>

OK, but why did you this?

It's simple: for some reason Android Framework was using the first instance of the viewpager (first viewpager on layout) and not recreating another.

SO??????

So the solution was to give specific ids for each viewpager/tablayout

:)

EDIT: OK, but how did you reach that conclusion? (Framework, instance, bla bla bla?)

Found the explanation here: https://groups.google.com/forum/#!topic/android-developers/cKdvKyneHYY

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

multiple view in one layout

From Dev

Using multiple controllers inside of one view

From Dev

How to use multiple layout using one RecyclerView Adapter?

From Dev

Using Multiple XML Layouts for One Activity

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

Keeping one fragment with multiple views using ViewPager

From Dev

Convert multiple tables to one table inside same DIV using jQuery

From Dev

Android Multiple Implementations of one element of the same Layout

From Dev

multiple layouts in one xml file

From Dev

positioning layouts with multiple includes

From Dev

Using multiple RecyclerView with one Adapter class

From Dev

Multiple RecyclerView in one Fragment

From Dev

ViewPager, same Fragment with RecyclerView for multiple tabs

From Dev

Replacing multiple fragmentclass using one fragment view

From Dev

Multiple models in one view using IEnumerable

From Dev

One view using multiple time in xib

From Dev

Lags in multiple layouts RecyclerView

From Dev

Multiple layouts in recyclerview

From Dev

Multiple UIViewControllers in "one" view

From Dev

One View for multiple models?

From Dev

Multiple UIViewControllers in "one" view

From Dev

RecyclerView ambiguos setVisibility function, clicking on one view affects multiple views

From Dev

Is it possible to have one activity with multiple xml layouts?

From Dev

Backbone/Marionette - handling multiple layouts for one region

From Dev

Multiple layouts for one Angular 2 UI Component

From Dev

How to change "date modified" of multiple folders, to be the same of that of a .mkv that's inside each one, using Cygwin

From Dev

Can one avoid multiple DOM elements with same id, when using Backbone/Marionette view instances?

From Dev

Merge multiple objects inside the same array into one object

Related Related

  1. 1

    multiple view in one layout

  2. 2

    Using multiple controllers inside of one view

  3. 3

    How to use multiple layout using one RecyclerView Adapter?

  4. 4

    Using Multiple XML Layouts for One Activity

  5. 5

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  6. 6

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  7. 7

    Keeping one fragment with multiple views using ViewPager

  8. 8

    Convert multiple tables to one table inside same DIV using jQuery

  9. 9

    Android Multiple Implementations of one element of the same Layout

  10. 10

    multiple layouts in one xml file

  11. 11

    positioning layouts with multiple includes

  12. 12

    Using multiple RecyclerView with one Adapter class

  13. 13

    Multiple RecyclerView in one Fragment

  14. 14

    ViewPager, same Fragment with RecyclerView for multiple tabs

  15. 15

    Replacing multiple fragmentclass using one fragment view

  16. 16

    Multiple models in one view using IEnumerable

  17. 17

    One view using multiple time in xib

  18. 18

    Lags in multiple layouts RecyclerView

  19. 19

    Multiple layouts in recyclerview

  20. 20

    Multiple UIViewControllers in "one" view

  21. 21

    One View for multiple models?

  22. 22

    Multiple UIViewControllers in "one" view

  23. 23

    RecyclerView ambiguos setVisibility function, clicking on one view affects multiple views

  24. 24

    Is it possible to have one activity with multiple xml layouts?

  25. 25

    Backbone/Marionette - handling multiple layouts for one region

  26. 26

    Multiple layouts for one Angular 2 UI Component

  27. 27

    How to change "date modified" of multiple folders, to be the same of that of a .mkv that's inside each one, using Cygwin

  28. 28

    Can one avoid multiple DOM elements with same id, when using Backbone/Marionette view instances?

  29. 29

    Merge multiple objects inside the same array into one object

HotTag

Archive