使用底部导航栏并将其隐藏在滚动条上。如何预防呢?

时间:2018-04-02 05:42:01

标签: android xml scroll bottomnavigationview android-collapsingtoolbarlayout

我在this library的Android中使用底部导航栏。

在我的应用程序中,出于显而易见的原因我需要隐藏工具栏,但不知怎的,我最终还隐藏了底部导航栏,我不想让它隐藏起来。 你有什么建议来实现这个目标? 简而言之, HIDE 工具栏,但不要隐藏底部导航栏?

app_bar_main.xml

的xml代码
   <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.android.uidesigns.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                />

    </android.support.design.widget.AppBarLayout>
   <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

content_main.xml

的xml代码
  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.android.uidesigns.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/vp_horizontal_ntb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <devlight.io.library.ntb.NavigationTabBar
            android:id="@+id/ntb_horizontal"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            app:ntb_badged="true"
            app:ntb_titled="true"
            app:ntb_active_color="@color/colorPrimary"
            app:ntb_inactive_color="@color/md_grey_500"
            app:ntb_title_mode="all"
            app:ntb_badge_position="right"
            app:ntb_badge_gravity="top"
            app:ntb_badge_bg_color="#CF000F"
            app:ntb_badge_title_color="#ffffffff"
            app:ntb_badge_use_typeface="true"
            app:ntb_swiped="true"
            app:ntb_bg_color="@color/transparent"
            app:ntb_icon_size_fraction="0.5"
            app:ntb_badge_size="9sp"
            app:ntb_title_size="11sp"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java 使用名为 navigationBar()的方法启动标签。

private void navigationBar() {

    final ViewPager pager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
    pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return new FeedsFragment();
                case 1: // Fragment # 1 - This will show SecondFragment
                    return new ConnectionFragment();
                case 2: // Fragment # 2 - This will show ThirdFragment
                    return new  WriteReviewFragment();
                case 3: // Fragment # 3 - This will show FourthFragment
                    return new NotificationFragment();
                case 4: // Fragment # 3 - This will show FourthFragment
                    return new TestFragment();
                default:
                    return new FeedsFragment();
            }
        }
        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            return "Page" + position;
        }
    });

    final String[] colors = getResources().getStringArray(R.array.from_colors);
    final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();

    models.add(
            new NavigationTabBar.Model.Builder(
                    getResources().getDrawable(R.drawable.rating_feed),
                    Color.parseColor(colors[0]))
                    .title("All Poems")
                    .badgeTitle(feedsNotificationValue)
                    .build()
    );
    models.add(
            new NavigationTabBar.Model.Builder(
                    getResources().getDrawable(R.drawable.conn),Color.parseColor(colors[0]))
                    .title("All Users")
                    .badgeTitle("3325")
                    .build()
    );
    models.add(
            new NavigationTabBar.Model.Builder(
                    getResources().getDrawable(R.drawable.review_one),Color.parseColor(colors[0]))
                    .title("Write Poem")
                    .badgeTitle("")
                    .build()
    );
    models.add(
            new NavigationTabBar.Model.Builder(
                    getResources().getDrawable(R.drawable.notification),Color.parseColor(colors[0]))
                    .title("Notifications")
                    .badgeTitle(notificationBadgeValue)
                    .build()
    );
    models.add(
            new NavigationTabBar.Model.Builder(
                    getResources().getDrawable(R.drawable.user_one),Color.parseColor(colors[0]))
                    .title("You")
                    .build()
    );
    navigationTabBar.setModels(models);
    navigationTabBar.setViewPager(viewPager, 0);
    navigationTabBar.setIsBadgeUseTypeface(true);
    navigationTabBar.canScrollHorizontally(-1);
    navigationTabBar.setTitleSize(10 * getResources().getDisplayMetrics().density); //Important line for scaling title size
    navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
            if (position == 3){
                appbar.setElevation(0f);
            }
            else if (position == 0 || position == 1 ||position == 2 || position == 4 ){
                appbar.setElevation(14f);
            }
        }

        @Override
        public void onPageSelected(final int position) {
            navigationTabBar.getModels().get(position).hideBadge();
        }

        @Override
        public void onPageScrollStateChanged(final int state) {

        }
    });
    //Important Code for hiding the padding
    navigationTabBar.post(new Runnable() {
        @Override
        public void run() {
            final View viewPager = findViewById(R.id.vp_horizontal_ntb);
            ((ViewGroup.MarginLayoutParams) viewPager.getLayoutParams()).bottomMargin =
                    (int) -navigationTabBar.getBadgeMargin();
            viewPager.requestLayout();
        }
    });
    navigationTabBar.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
                final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
                navigationTabBar.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (model.getTitle().toString().equals("Somthing")||model.getTitle().toString().equals("Write Poem")||model.getTitle().toString().equals("Different")){
                            model.hideBadge();
                        }
                        else
                        model.showBadge();

                    }
                }, i * 180);
            }
        }
    }, 500);
}

随意询问您是否需要额外的东西。

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#423752"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:title="@string/app_name"
            app:expandedTitleTextAppearance="@null"
            app:contentScrim="#605271"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:background="#605271"
                android:src="@drawable/ic_eighth"
                android:tint="#9f90af"
                android:padding="32dp"
                app:layout_collapseMode="pin"/>

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:title="@string/app_name"
                app:popupTheme="@style/AppTheme.AppBarOverlay"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_horizontal_ntb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <devlight.io.library.ntb.NavigationTabBar
        android:id="@+id/ntb_horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        app:ntb_badge_size="12sp"
        app:ntb_badge_bg_color="#ffff0000"
        app:ntb_badge_gravity="top"
        app:ntb_badge_position="left"
        app:ntb_badge_title_color="#ffffffff"
        app:ntb_badge_use_typeface="true"
        app:ntb_badged="true"
        app:ntb_typeface="fonts/agency.ttf"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="76dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:tint="#9f90af"
        android:src="@drawable/ic_first"
        app:backgroundTint="#605271"/>

</android.support.design.widget.CoordinatorLayout>

并且对于HIDE和SHOW,您可以使用NavigationTabBar更改setBehaviorEnabled(boolean);.     对于AUTO HIDE和SHOW:navigationTabBar.setBehaviorEnabled(true);     对于理想的AUTO HIDE和SHOW功能:navigationTabBar.setBehaviorEnabled(false);