android - 带工具栏的 Viewpager

标签 android android-layout android-fragments

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

这就是我在 MainActivity 和 viewpager_layout.xml 中创建栏的原因

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

主要 Activity

@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>

我应该怎么做才能在每个页面上使用我的栏。 我是否必须在每个 fragment 中创建栏?

最佳答案

我意识到这是因为我使用了 <include>添加 ListView从另一个布局资源文件到我的 fragment 。

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

关于android - 带工具栏的 Viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074967/

相关文章:

android - 在android中按大小排序文件列表

android - 在 Android 中实现切片

android - 如何更改 ImageView 中图像的颜色?

java - 如何更改Android中的Fragment

java - Guava 中的 ListenableFuture 将在哪个线程中调用 FutureCallback?

android - viewpager 中的 fragment 不显示何时在 recyclerview 中

android - 自定义具有不同背景颜色的操作栏 - Android

Android 在 FragmentPagerAdapter 中的 Fragment 中设置 TextView 的文本

android - 不要在 fragment 中获取 rootLayoutContainer(Android 3.0 预览版)

android - 用于后台任务的线程与适配器——更喜欢哪一个,为什么?