如何在底部导航和Viewpager中使用TabLayout?

时间:2018-08-06 04:28:28

标签: android kotlin android-viewpager android-tablayout bottomnavigationview

我使用bottom navigation bar创建了一个活动。 当我想切换到另一个片段时,我想要tablayout。 并且tablayout可以控制viewpager。我可以将viewpager放入指定的片段吗?

我想这样:

enter image description here

这是当前结果。我想将viewpager放在红色框中。

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用此方法将您的BottomNavigationViewTabLayoutViewPager

    myBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            int id = item.getItemId();

            if (id == R.id.action_one) {
                myViewPager.setCurrentItem(0);
            }else if (id == R.id.action_two) {
                myViewPager.setCurrentItem(1);
            }else if (id == R.id.action_three) {
                myViewPager.setCurrentItem(2);
            }else if (id == R.id.action_four) {
                myViewPager.setCurrentItem(3);
            }
            return false;
        }
    });


    myViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == 0) {
                myBottomNavigationView.setSelectedItemId(R.id.action_one);
            }else if (position == 1) {
                myBottomNavigationView.setSelectedItemId(R.id.action_two);
            }else if (position == 2) {
                myBottomNavigationView.setSelectedItemId(R.id.action_three);
            }else if (position == 3) {
                myBottomNavigationView.setSelectedItemId(R.id.action_four);
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

答案 1 :(得分:1)

您需要将View pager放在Activity上。制作一个视图寻呼机适配器。将不同的片段连接到适配器中。之后,您可以在“活动”示例中设置适配器,如下所示。抱歉,我们需要将标签布局放在vewPager之前。在您的View Pager Adapter上连接标签布局。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/assignment_activitly"
>
<android.support.design.widget.TabLayout
       android:id="@+id/tablayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:tabMode="fixed"
       android:background="@color/colorPrimary"
       app:tabTextColor="@color/textPrimary"
       app:tabSelectedTextColor="@color/colorAccent"
       app:tabTextAppearance="@style/Tab"
       />
   <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@android:color/white"
       />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
   ViewPager viewpager;
   TabLayout tabLayout;
   public void onCreate(Bundle savedInstanceState) {
   viewpager = findViewById(R.id.viewpager);
   tabLayout = findViewById(R.id.tablayout);
   super.onCreate(saveedInstanceState);
   .........
   ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
   viewpager.setViewPager(adapter);
   tabLayout.setupWithViewPager(viewPager);     
   }
}

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter {
 private String tabTitles [] = new String[] {"title first", "title Second"};
  public ViewPagerAdapter(FragmentManager fm) {
    super(fm);

}

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new YourFragmentFirst();
    } else if (....){
        return new YourFragmentSeconde();
    }
    ........  
}

@Override
public int getCount() {
    return  //return your number of Fragment;
}

@Override
public CharSequence getPageTitle(int position) {
    return tabTitles[position];
}
}