简体   繁体   中英

Fragments with TabLayout and ViewPager

At the moment I have MainActivity with 2 tabs(with two fragments) and Navigation View with 3 items of menu. I have only one activity with tabs for now. In MainActivity I have initialization of a Toolbar, NavigationView, ViewPager, TabLayout. Also I have one instance of adapter here, which create fragments for tabs.

When I select one of menu's item, I want it to open a new fragment with two other tabs (with two other fragments).

How can realize it with fragments? Or better use additional Activity?

NavigationView

MainActivity with two tabs

activity_main_xml:

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

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


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"

            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight = "6dp"

            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/white"
            />

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


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        />

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






<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/navigationView"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/menu_navigation"

/>

MainActivity:

package ru.alexbykov.sailesstat;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import ru.alexbykov.sailesstat.statistic.adapter.TheSalesTabsFragmentAdapter;

public class MainActivity extends AppCompatActivity {


    private static final int LAYOUT = R.layout.activity_main;

    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ViewPager viewPager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppDefault);
        super.onCreate(savedInstanceState);
        setContentView(LAYOUT);


        setupToolBar();
        setupNavigationView();
        setupTabs();


    }

    private void setupToolBar() {


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.app_name);

    }

    private void setupNavigationView() {

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        ActionBarDrawerToggle togle =
                new ActionBarDrawerToggle(
                        this,
                        drawerLayout,
                        toolbar,
                        R.string.view_navigation_open,
                        R.string.view_navigation_close);

//     drawerLayout.setDrawerListener(togle);

        togle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {


                drawerLayout.closeDrawers();

                switch (item.getItemId())
                {


                }




                return true;
            }
        });


    }

/*    private void startTendersFragment() {


        fTrans = getSupportFragmentManager().beginTransaction();;
        TendersFragment tendersFragment = new TendersFragment();

        fTrans
                .add(R.id.frameLayout, tendersFragment)
                .addToBackStack(null)
                .commit();


    }*/




    private void setupTabs() {


        viewPager = (ViewPager) findViewById(R.id.viewPager);
        TheSalesTabsFragmentAdapter adapter = new TheSalesTabsFragmentAdapter(this, getSupportFragmentManager());
        viewPager.setAdapter(adapter);

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

    }




}

TheSalesTabsFragmentAdapter

   package ru.alexbykov.sailesstat.statistic.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.HashMap;
import java.util.Map;

import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.ManagersFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.PlanFragment;

/**
 * Created by Alexey on 09.06.2016.
 */
public class TheSalesTabsFragmentAdapter extends FragmentPagerAdapter {




    //for use strings
    private Context context;
    private Map<Integer, AbstractTabFragment> tabs;



    public TheSalesTabsFragmentAdapter(Context context, FragmentManager fm) {
        super(fm);

        this.context = context;
        initTabs();
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {

        return tabs.get(position).getTitle();
    }

    private void initTabs() {

        tabs = new HashMap<>();

            tabs.put(0, PlanFragment.getInstance(context));
            tabs.put(1, ManagersFragment.getInstance(context));

    }
}

ManagersFragment

package ru.alexbykov.sailesstat.statistic.fragments;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

public class AbstractTabFragment extends Fragment {


    private String title;
    protected Context context;
    protected View view;


    public void setTitle(String title) {
        this.title = title;
    }

    public void setContext(Context context) {

        this.context=context;

    }

    public String getTitle() {
        return title;
    }
}

PlanFragment

package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class PlanFragment extends AbstractTabFragment {

    private static final int LAYOUT = R.layout.fragment_plan;





    public static PlanFragment getInstance(Context context) {


/*        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);*/


        PlanFragment fragment = new PlanFragment();
        fragment.setContext(context);
        fragment.setTitle(context.getString(R.string.tab_plan_stat_fragment));

        return fragment;


    }

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

        view = inflater.inflate(LAYOUT, container, false);
        return view;
    }








}

AbstractTabFragment

package ru.alexbykov.sailesstat.statistic.fragments;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

public class AbstractTabFragment extends Fragment {


    private String title;
    protected Context context;
    protected View view;


    public void setTitle(String title) {
        this.title = title;
    }

    public void setContext(Context context) {

        this.context=context;

    }

    public String getTitle() {
        return title;
    }
}

First off fragments are not the best practice ever as are NavigationViews. I would recommend buttons to switch activities with no use of fragments at all. If I understand you right you have your main page with content, in which I would add three buttons, each with an intent to start another activity in leiu of a NavigationView. Each of these activities would have their own buttons which would lead you to whatever activity you want to go to from there.

You can add new fragment and inside that add viewpager2. May be this will help you.

http://logicrider.com/blogs/viewpager2_with_fragments_and_tablayout_android_studio.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM