简体   繁体   English

使用 TabLayout 和 ViewPager 的片段

[英]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.目前,我有带有 2 个选项卡(带有两个片段)的 MainActivity 和带有 3 个菜单项的导航视图。 I have only one activity with tabs for now.我现在只有一项带有标签的活动。 In MainActivity I have initialization of a Toolbar, NavigationView, ViewPager, TabLayout.在 MainActivity 中,我初始化了 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带有两个选项卡的 MainActivity

activity_main_xml: 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 TheSalesTabsFragment适配器

   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. 如果我理解你,你的主页面上有内容,我会在其中添加三个按钮,每个按钮都打算在一个NavigationView的leiu中启动另一个活动。 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.您可以添加新片段并在其中添加 viewpager2。 May be this will help you.可能这会对你有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有TabLayout的Android ViewPager片段行为异常 - Android ViewPager Fragments With TabLayout Acting unexpectedly 使用ViewPager / TabLayout在两个片段上同步微调器 - Synchronizing Spinner over two Fragments with ViewPager/TabLayout 使用ViewPager和TabLayout切换选项卡时,片段不可见 - Fragments not visible when switch tabs using ViewPager and TabLayout ViewPager不显示在TabLayout中使用FragmentPagerAdapter添加的所有片段 - ViewPager not showing all Fragments added using FragmentPagerAdapter in TabLayout Android:将数据从由 ViewPager2 和 TabLayout 管理的片段发送到包含的活动 - Android: send data from fragments managed by a ViewPager2 and TabLayout to the containing activity 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 我在viewpager中有布局,如何在第一个片段以外的4个片段中隐藏Bottombar? - I have tablayout with viewpager how to hide Bottombar in 4 Fragments except first Fragment? 如何初始化在 Activity 中定义的接口,该接口由通过 TabLayout 使用 ViewPager 设置的 3 个片段实现? - How to initialize an interface defined in an Activity which is implemented by 3 fragments setup with ViewPager via TabLayout? 在 TabLayout 和 ViewPager2 中执行异步任务后更新具有相同布局的多个片段 - Update multiple fragments with same layout after doing Async Task in TabLayout and ViewPager2 ViewPager还是片段? - ViewPager or Fragments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM