如何在同一活动中实现TabLayout和底部导航视图

时间:2019-10-07 14:23:19

标签: android android-tablayout bottomnavigationview

嗨,朋友们,我必须创建一个具有3个菜单项(菜单1,菜单2,菜单3)的底部导航视图的活动,并且同一活动中应该有一个布局。每个菜单项都有两个标签。我遵循了前面的问题,例如How to use tablayout with bottom navigation and viewpager? 但不成功附加代码,请参阅并告诉我如何实现。 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
    />
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewpager"
    android:layout_above="@+id/bot_nav"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/frame"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bot_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottomnav_menu"
       />
</RelativeLayout>

Mainactivity.java

public class MainActivity extends AppCompatActivity {

    TabLayout tablayout;
    BottomNavigationView bot_nav;
    FrameLayout frameLayout;
    ViewPager viewpager;

  //created 2 fragments for each menu item for 2 tabs
menu1_fragment fragment1;  //fragment for menu1 for tab1
menu2_fragment fragment2; //fragment for menu2 for tab1
menu3_fragment fragment3; //fragment for menu3 for tab1
menu1_fragment2 frag1;  //fragment for menu1 for tab2
menu2_fragment2 frag2; //fragment for menu2 for tab2
menu3_fragment2 frag3; //fragment for menu3 for tab2
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        tablayout = (TabLayout)findViewById( R.id.tabs );

        bot_nav =(BottomNavigationView)findViewById( R.id.bot_nav );
        frameLayout= (FrameLayout) findViewById( R.id.frame );

        fragment1 = new menu1_fragment();     
        fragment2 = new menu2_fragment();
        fragment3 = new menu3_fragment();

        frag1= new menu1_fragment2();
        frag2 = new menu2_fragment2();
        frag3= new menu3_fragment2();

        tablayout.addTab( tablayout.newTab().setText( "Tab1" ) );
        tablayout.addTab( tablayout.newTab().setText( "Tab2" ) );
        tablayout.setTabGravity( TabLayout.GRAVITY_FILL );

        viewpager =(ViewPager)findViewById( R.id.viewpager );
        viewPagerAdapter adapter= new viewPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter( adapter );

        viewpager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i) {
                if(position == 0){
                    bot_nav.setSelectedItemId( R.id.nav_menu1 );
                }else if(position == 1){
                    bot_nav.setSelectedItemId( R.id.nav_menu2 );
                }else if(position == 2){
                    bot_nav.setSelectedItemId( R.id.nav_Menu3 );
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        } );

        bot_nav.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nav_menu1:
                        viewpager.setCurrentItem( 0 );
                        break;
                    case R.id.nav_menu2:
                        viewpager.setCurrentItem( 1 );
                        break;
                    case R.id.nav_Menu3:
                        viewpager.setCurrentItem( 2 );
                        break;
                }
                return false;
            }
        } );

    }
}

ViewpagerAdapter.java

public class viewPagerAdapter extends FragmentPagerAdapter {
    private Context mycontext;
    int totaltabs;
    public viewPagerAdapter(FragmentManager fm) {
        super( fm );
        //mycontext = context;
        this.totaltabs= totaltabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                menu1_fragment menu1Fragment = new menu1_fragment();
                return menu1Fragment;
            case 1:
                menu1_fragment2 menu1Fragment2 = new menu1_fragment2();
                return menu1Fragment2;
            default:
                return null;
        }
    }

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

添加了选项卡和底部导航,但是得到的结果只是一个空白片段没有显示。 附上我创建的布局图片和文件,请参见并获得帮助

Here is the layout image

these are the file structure i have created

0 个答案:

没有答案