wake-up-neo.com

Android TabLayout in Fragment

Ich verwende Navigation Drawer, um zwischen Fragmenten zu navigieren, die alle die MainActivity-Symbolleiste verwenden, aber jedes Fragment hat seine eigene TabLayout mit 3 Registerkarten. In jeder Registerkarte verwende ich RecyclerView, um unterschiedlichen Text und Bilder anzuzeigen.

Ich habe dies letztes Mal mit Aktivitäten anstelle von Fragmenten gemacht, aber jetzt mit der Schublade ist es besser, Fragmente zu verwenden. 

Ich bin an einem Punkt angelangt, an dem die Registerkarten funktionieren, aber sobald ich die Variable ViewPager einsetze und die Adapter mit den Registerkarten verbinde, und ich das neue Fragment aus der Schublade öffne, bekomme ich einen Nullzeiger. Was könnte das Problem sein? 

Hauptaktivität.Java

public class MainActivity extends AppCompatActivity

    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return id == R.id.action_settings || super.onOptionsItemSelected(item);

}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    Fragment fragment;

    if (id == R.id.fragment_one) {
        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    } else if (id == R.id.fragment_two) {

        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    } else if (id == R.id.fragment_three) {

        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

activity_main.xml

<Android.support.v4.widget.DrawerLayout 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/drawer_layout"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true"
tools:openDrawer="start">

<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.xcandy.guideforfifa17.MainActivity">

    <Android.support.design.widget.AppBarLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:theme="@style/AppTheme.AppBarOverlay">

        <Android.support.v7.widget.Toolbar
            Android:id="@+id/toolbar"
            Android:layout_width="match_parent"
            Android:layout_height="?attr/actionBarSize"
            Android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

<Android.support.design.widget.NavigationView
    Android:id="@+id/nav_view"
    Android:layout_width="wrap_content"
    Android:layout_height="match_parent"
    Android:layout_gravity="start"
    Android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</Android.support.v4.widget.DrawerLayout>

NewFragment.Java

public class NewFragment extends Fragment {

private RecyclerView mRecyclerView;


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

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("A"));
    tabLayout.addTab(tabLayout.newTab().setText("B"));
    tabLayout.addTab(tabLayout.newTab().setText("C"));

    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);


    viewPager.setAdapter(new PagerAdapter
            (getFragmentManager(), tabLayout.getTabCount()));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    return view;
}

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }



    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:

                RecyclerView.Adapter mAdapter = new AdapterOne();
                mRecyclerView.setAdapter(mAdapter);

            case 1:

                mAdapter = new AdapterTwo();
                mRecyclerView.setAdapter(mAdapter);

            case 2:

                mAdapter = new AdapterThree();
                mRecyclerView.setAdapter(mAdapter);

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}
}

activity_fragment.xml

<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=".NewFragment">

<Android.support.design.widget.AppBarLayout
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:theme="@style/AppTheme.AppBarOverlay">

    <Android.support.design.widget.TabLayout
        Android:id="@+id/tabs"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

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

<Android.support.v4.view.ViewPager
    Android:id="@+id/viewpager"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:background="@Android:color/white"/>

<Android.support.v7.widget.RecyclerView
    Android:id="@+id/recycler_view"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    />

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

Fehlerprotokoll

                  --------- beginning of crash
06-23 11:19:56.985 2466-2466/com.candyx.sims E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.candyx.sims, PID: 2466
                                                               Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v7.widget.RecyclerView.setLayoutManager(Android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                   at com.candyx.sims.BlankFragment.onCreateView(BlankFragment.Java:38)
                                                                   at Android.support.v4.app.Fragment.performCreateView(Fragment.Java:1974)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1067)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1252)
                                                                   at Android.support.v4.app.BackStackRecord.run(BackStackRecord.Java:742)
                                                                   at Android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1617)
                                                                   at Android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.Java:517)
                                                                   at Android.os.Handler.handleCallback(Handler.Java:739)
                                                                   at Android.os.Handler.dispatchMessage(Handler.Java:95)
                                                                   at Android.os.Looper.loop(Looper.Java:135)
                                                                   at Android.app.ActivityThread.main(ActivityThread.Java:5254)
                                                                   at Java.lang.reflect.Method.invoke(Native Method)
                                                                   at Java.lang.reflect.Method.invoke(Method.Java:372)
                                                                   at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
                                                                   at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
06-23 11:23:14.912 5839-5873/com.candyx.sims D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                               [ 06-23 11:23:14.920  5839: 5839 D/         ]
                                                               HostConnection::get() New Host Connection established 0xb4125190, tid 5839
06-23 11:23:14.927 5839-5839/com.candyx.sims D/Atlas: Validating map...
06-23 11:23:15.011 5839-5873/com.candyx.sims I/OpenGLRenderer: Initialized EGL, version 1.4
06-23 11:23:15.027 5839-5873/com.candyx.sims D/OpenGLRenderer: Enabling debug mode 0
06-23 11:23:15.039 5839-5873/com.candyx.sims W/EGL_emulation: eglSurfaceAttrib not implemented
06-23 11:23:15.039 5839-5873/com.candyx.sims W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae838600, error=EGL_SUCCESS
06-23 11:23:16.444 5839-5839/com.candyx.sims W/PathParser: Points are too far apart 4.000000596046461
06-23 11:23:17.342 5839-5839/com.candyx.sims W/PathParser: Points are too far apart 4.000000596046461
06-23 11:23:17.350 5839-5839/com.candyx.sims I/AppCompatViewInflater: app:theme is now deprecated. Please move to using Android:theme instead.
06-23 11:23:17.369 5839-5839/com.candyx.sims D/AndroidRuntime: Shutting down VM
06-23 11:23:17.369 5839-5839/com.candyx.sims E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.candyx.sims, PID: 5839
                                                               Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v7.widget.RecyclerView.setLayoutManager(Android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                   at com.candyx.sims.NewFragment.onCreateView(NewFragment.Java:38)
                                                                   at Android.support.v4.app.Fragment.performCreateView(Fragment.Java:1974)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1067)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1252)
                                                                   at Android.support.v4.app.BackStackRecord.run(BackStackRecord.Java:742)
                                                                   at Android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1617)
                                                                   at Android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.Java:517)
                                                                   at Android.os.Handler.handleCallback(Handler.Java:739)
                                                                   at Android.os.Handler.dispatchMessage(Handler.Java:95)
                                                                   at Android.os.Looper.loop(Looper.Java:135)
                                                                   at Android.app.ActivityThread.main(ActivityThread.Java:5254)
                                                                   at Java.lang.reflect.Method.invoke(Native Method)
                                                                   at Java.lang.reflect.Method.invoke(Method.Java:372)
                                                                   at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
                                                                   at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
06-23 11:25:16.961 7518-7518/com.candyx.sims W/art: Before Android 4.1, method Android.graphics.PorterDuffColorFilter Android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(Android.graphics.PorterDuffColorFilter, Android.content.res.ColorStateList, Android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in Android.graphics.drawable.Drawable
06-23 11:25:17.078 7518-7548/com.candyx.sims D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                               [ 06-23 11:25:17.081  7518: 7518 D/         ]
                                                               HostConnection::get() New Host Connection established 0xb412a1f0, tid 7518
06-23 11:25:17.082 7518-7518/com.candyx.sims D/Atlas: Validating map...
06-23 11:25:17.109 7518-7548/com.candyx.sims I/OpenGLRenderer: Initialized EGL, version 1.4
06-23 11:25:17.126 7518-7548/com.candyx.sims D/OpenGLRenderer: Enabling debug mode 0
06-23 11:25:17.134 7518-7548/com.candyx.sims W/EGL_emulation: eglSurfaceAttrib not implemented
06-23 11:25:17.134 7518-7548/com.candyx.sims W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb41234c0, error=EGL_SUCCESS
06-23 11:25:31.030 7518-7518/com.candyx.sims W/PathParser: Points are too far apart 4.000000596046461
06-23 11:25:32.174 7518-7518/com.candyx.sims W/PathParser: Points are too far apart 4.000000596046461
06-23 11:25:32.187 7518-7518/com.candyx.sims I/AppCompatViewInflater: app:theme is now deprecated. Please move to using Android:theme instead.
06-23 11:25:32.207 7518-7518/com.candyx.sims D/AndroidRuntime: Shutting down VM
06-23 11:25:32.207 7518-7518/com.candyx.sims E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.candyx.sims, PID: 7518
                                                               Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v7.widget.RecyclerView.setLayoutManager(Android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                   at com.candyx.sims.NewFragment.onCreateView(NewFragment.Java:38)
                                                                   at Android.support.v4.app.Fragment.performCreateView(Fragment.Java:1974)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1067)
                                                                   at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1252)
                                                                   at Android.support.v4.app.BackStackRecord.run(BackStackRecord.Java:742)
                                                                   at Android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1617)
                                                                   at Android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.Java:517)
                                                                   at Android.os.Handler.handleCallback(Handler.Java:739)
                                                                   at Android.os.Handler.dispatchMessage(Handler.Java:95)
                                                                   at Android.os.Looper.loop(Looper.Java:135)
                                                                   at Android.app.ActivityThread.main(ActivityThread.Java:5254)
                                                                   at Java.lang.reflect.Method.invoke(Native Method)
                                                                   at Java.lang.reflect.Method.invoke(Method.Java:372)
                                                                   at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
                                                                   at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
15
Jacques Krause

Nachdem ich Ihren Code durchgesehen hatte, stellte ich fest, dass Sie vergessen haben, ein Fragment in der getItem - Methode von PagerAdapter zurückzugeben, das Sie vom FragmentStatePagerAdapter überschrieben haben. In jedem Fall gibt es keine break-Anweisung, aufgrund derer jedes Mal ein Nullwert zurückgegeben wurde.

Dazu müssen Sie für jede Seite in ViewPager ein weiteres neues Fragment erstellen, oder Sie können ein neues Fragment erstellen, das auf allen ViewPager-Seiten verwendet werden kann.

Sie können nicht dasselbe Recycling-Fenster in den Registerkarten des View-Pagers verwenden, die Sie im übergeordneten Fragment definiert haben. 

activity_fragment_one.xml

 <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=".NewFragment">

    <Android.support.design.widget.AppBarLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:theme="@style/AppTheme.AppBarOverlay">

        <Android.support.design.widget.TabLayout
            Android:id="@+id/tabs"
            Android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:tabTextAppearance="?android:attr/textAppearanceMedium"
            app:tabTextColor="#ffffff"
            Android:layout_height="?android:actionBarSize"
            app:tabMode="fixed"
            app:tabGravity="fill"/>

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

    <Android.support.v4.view.ViewPager
        Android:id="@+id/viewpager"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        Android:background="@Android:color/white"/>
</Android.support.design.widget.CoordinatorLayout>

Ich habe Ihr Fragment behoben und ein neues Fragment erstellt, das Sie in ViewPager Pages verwenden können.

public class NewFragment extends Fragment {

private RecyclerView mRecyclerView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_fragment_one, container, false);
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("A"));
    tabLayout.addTab(tabLayout.newTab().setText("B"));
    tabLayout.addTab(tabLayout.newTab().setText("C"));
    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
   // mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    //mRecyclerView.setLayoutManager(mLayoutManager);
    viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount()));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    return view;
}

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }


    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
              return new FragmentTab();
            case 1:
                return new FragmentTab();
            case 2:
                return new FragmentTab();

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}
}

FragmentTab für jede Seite in View Pager (oder Sie können für jede Seite je nach Bedarf ein anderes Fragment erstellen).

public class FragmentTab extends Fragment {
RecyclerView mRecyclerView;

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

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
    RecyclerView.Adapter mAdapter = new AdapterOne();
    mRecyclerView.setAdapter(mAdapter);
    return view;
}
}

fragment_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical" Android:layout_width="match_parent"
    Android:layout_height="match_parent">
    <Android.support.v7.widget.RecyclerView
        Android:id="@+id/recycler_view"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        />


</LinearLayout>
21
Aman Jain

Verwenden Sie getChildFragmentManager() anstelle von getSupportFragmentManager()

4
madhu sudhan

Ohne die Fehlerprotokolle ist es schwierig zu sagen, wo und warum Sie das NullPointerException erhalten.

Das erste, was meine Aufmerksamkeit auf sich zog, ist, dass Sie Ihre TabLayout und ViewPager nicht verlinkt haben. Verwenden Sie den folgenden Code, nachdem Sie den Adapter für ViewPager festgelegt haben:

// setUpWithViewPager doesn't work without using a Runnable interface.
// Support library bug, maybe?
tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

Ich bin nicht sicher, ob dies das Problem ist, aber probieren Sie es aus :)

2

ich schlage vor, getChildFragmentManager anstelle von getFragmentManager in zu verwenden

viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount()));

1
masoud vali

Nun, vor einiger Zeit habe ich genau das gleiche App-Design implementiert. Ich hatte ein Fragment, das Tabs und ViewPager enthalten sollte. Das war eine knifflige Aufgabe, und ich erinnere mich, dass ich ähnliche Probleme hatte wie Sie. So habe ich es gelöst:

Die Layoutreihenfolge lautet: 

Activity -> Fragment -> Tab Layout + ViewPager -> FragmentStatePagerAdapter -> Fragment -> RecyclerView

Sie haben nichts mit Ihrer Hauptaktivität zu tun. Das ganze Zeug ist in Fragmenten. Befreien Sie sich von TabLayout in Ihrem activity_main.xml

Sie müssen mit verschachtelten Fragmenten in Pager umgehen. Der gesamte Seiteninhalt muss vorhanden sein und TabLayout muss sich in demselben Layout befinden, in dem sich ViewPager befindet.  

Mein Projekt ist ein bisschen alt und ich habe Android.support.v4.app.Fragment verwendet, falls es sinnvoll ist. Ich habe Quellen aus Daten entfernt, die Sie nicht benötigen, daher werden möglicherweise verwaiste Methoden angezeigt.

Die Aufgabe bestand darin, die Preisliste mit Kategorien anzuzeigen und jedes Kategorieprodukt auf einer eigenen Seite darzustellen, so dass horizontale Wischbewegungen die Kategorien und vertikale Wischbewegungen - Listenprodukte - wechseln würden. 

Jedes Fragment mit ViewPager und TabLayout hat folgendes subfragment_price_list_pager.xml-Layout:

<?xml version="1.0" encoding="utf-8"?>
<Android.support.v7.widget.CardView 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:background="@color/icons"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp"
    app:cardPreventCornerOverlap="true">

    <Android.support.v7.widget.Toolbar
        Android:id="@+id/priceListToolbar"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:elevation="4dp"
        Android:theme="@style/AppTheme.Toolbar.TabLayout"
        app:navigationIcon="@drawable/ic_tag_multiple_grey600_24dp"
        tools:ignore="UnusedAttribute">

        <Android.support.design.widget.TabLayout
            Android:id="@+id/tabBar"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_alignParentLeft="true"
            Android:layout_alignParentStart="true" />
    </Android.support.v7.widget.Toolbar>

    <Android.support.v4.view.ViewPager
        Android:id="@+id/priceList"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginTop="58dp"
        Android:padding="5dp"
        Android:scrollbars="horizontal" />


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

Hier ist der PriceListPagerFragment.class:

public class PriceListPagerFragment extends Fragment {
    private static final String TAG = "PriceListPagerFragment";
    private PriceList mPriceList;

    private ViewPager mCategoriesViewPager;
    private TabLayout tabLayout;

    private Loader<PriceList> priceListLoader;
    private EditText scanerBarcodeText;
    private OnCartActionListener mListener;

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

        // Pager that would be inflated with page fragments.
        mCategoriesViewPager = (ViewPager) rootView.findViewById(R.id.priceList);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tabBar);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        // Do all the stuff with pager here:
        updatePriceListPages();

        return rootView;
    }


    private void updatePriceListPages() {
        if (mCategoriesViewPager != null && mPriceList != null) {
            Log.d(TAG, this + "updatePriceListPages");
            if (mCategoriesViewPager.getAdapter() == null) {


                PriceListCategoriesAdapter adapter = new PriceListCategoriesAdapter(
                        getFragmentManager(),
                        tabLayout
                );
                mCategoriesViewPager.setAdapter(
                        adapter
                );
                // Each page is a content of a single category of one-level catalog 
                // We did not supply catalog to adapter before it is bound to ViewPager for some serious reason which I can't recall now 
                adapter.setPriceList(mPriceList);

                tabLayout.setupWithViewPager(mCategoriesViewPager);
            } else {
                ((PriceListCategoriesAdapter) mCategoriesViewPager.getAdapter()).setPriceList(mPriceList);
            }
        }
    }
}

Benutzerdefinierte FragmentStatePagerAdapter-Implementierung, PriceListCategoriesAdapter.class:

public class PriceListCategoriesAdapter extends FragmentStatePagerAdapter {
    private static final String TAG = "PriceListCategoriesAd";
    private final TabLayout mTabLayout;
    private FragmentManager mFm;
    private Vector<PriceListFragment> recyclerViewList;
    private Vector<String> titles;

    public PriceListCategoriesAdapter(FragmentManager fm, TabLayout tabLayout) {
        super(fm);
        mFm = fm;
        mTabLayout = tabLayout;
        recyclerViewList = new Vector<>();
        titles = new Vector<>();
    }


    // All the magic with nested page fragments is done here
    public void setPriceList(PriceList priceList) {
        boolean updateTabs = false;

        Vector<String> newTitles = new Vector<>();

        int position = 0;
        for (CatalogItem catalogItem : priceList.getCatalogs()) {

            if (catalogItem.getProducts() == null)
                continue;

            boolean newFragment;

            PriceListFragment fragment;
            try {
                fragment = recyclerViewList.get(position);
                newFragment = false;

            } catch (ArrayIndexOutOfBoundsException e) {
                fragment = new PriceListFragment();
                newFragment = true;
            }

            fragment.setCatalogItem(catalogItem);

            newTitles.add(catalogItem.getName());

            if (newFragment) {
                recyclerViewList.add(fragment);
            }
            position++;
        }

        if (titles.size() != newTitles.size()) {
            updateTabs = true;
        } else {
            for (int position = 0; position < titles.size(); position++) {
                if (!titles.get(position).equals(newTitles.get(position))) {
                    updateTabs = true;
                    break;
                }
            }
        }

        titles = newTitles;
        notifyDataSetChanged();

        if (updateTabs)
            mTabLayout.setTabsFromPagerAdapter(this);
    }


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

    @Override
    public int getItemPosition(Object object) {
        PriceListFragment fragment = (PriceListFragment) object;
        String title = (String) fragment.getTitle();
        int position = titles.indexOf(title);

        if (position >= 0) {
            return position;
        } else {
            return POSITION_NONE;
        }
    }

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

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

    // We don't keep states
    @Override
    public Parcelable saveState() {
        return null;
    }
    // We don't keep states
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }
}

Und schließlich PriceListFragment Layout:

<?xml version="1.0" encoding="utf-8"?>
<Android.support.v7.widget.RecyclerView Android:id="@+id/categoryList"
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:scrollbars="vertical" />

PriceListFragment.class selbst hat nichts Besonderes. Instanzieren Sie einfach RecyclerView mit den richtigen Daten. 

Diese Methode getItem in PagerAdapter ist falsch, es wird Fragment init.__ zurückgegeben. Sie sollten Fragment zurückgeben. Und in Fragment setViewrecyclerView dazu

0
Phuoc Huynh