Viewpager与工具栏

时间:2016-05-06 14:28:16

标签: android android-layout android-fragments

我有一个包含菜单的栏。我正在使用视图栏,我需要在每一页显示我的栏。

这就是为什么我在MainActivity和viewpager_layout.xml

中创建了我的栏

如果我使用相同的片段布局,每个页面都运行良好,但是当我尝试更改布局时,它很糟糕。这是我的代码。

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout);
    ViewPager vpPager = (ViewPager) findViewById(R.id.viewPager);
    adapterViewPager = new viewPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);
    vpPager.setCurrentItem(0);

    getBottomBar();

    // Attach the page change listener inside the activity
    vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {

            Toast.makeText(MainActivity.this,
                    "Seçilen sayfa: " + position, Toast.LENGTH_SHORT).show();

            if (position == 0)
            {
                bottomNavigationBar.selectTab(0);
            }
            if (position == 1)
            {
                bottomNavigationBar.selectTab(1);
            }
        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // Code goes here
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });
}
BottomNavigationBar bottomNavigationBar;
private void getBottomBar() {
    bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

    bottomNavigationBar
            .addItem(new BottomNavigationItem(R.drawable.home, "Anasayfa"))
            .addItem(new BottomNavigationItem(R.drawable.group, "Gruplar"))
            .addItem(new BottomNavigationItem(R.drawable.counter, "Votloc"))
            .addItem(new BottomNavigationItem(R.drawable.date, "Profil"))
            .addItem(new BottomNavigationItem(R.drawable.password, "Daha Fazla"))
            .initialise();
    bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
        @Override
        public void onTabSelected(int position) {
        }
        @Override
        public void onTabUnselected(int position) {
        }
        @Override
        public void onTabReselected(int position) {
        }
    });
    bottomNavigationBar
            .setMode(BottomNavigationBar.MODE_FIXED);
    bottomNavigationBar
            .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
    bottomNavigationBar
            .setActiveColor(R.color.colorPrimary)
            .setInActiveColor("#FFFFFF")
            .setBarBackgroundColor("#FF1717");


}

viewpager_layout.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">
<com.ashokvarma.bottomnavigation.BottomNavigationBar
    android:layout_gravity="bottom"
    android:id="@+id/bottom_navigation_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</LinearLayout>

我应该怎样做才能在每一页上使用我的酒吧。 我是否必须在每个碎片中创建条形图?

1 个答案:

答案 0 :(得分:1)

我意识到这是因为我使用<include>ListView添加到我的片段中来自另一个布局资源文件。

之后我创建了一个新的布局文件,我只是将ListView直接包含在新的布局文件中。然后我将这个新的布局文件用于myMainFragment.java文件,最后我的ViewPager效果很好。