简体   繁体   中英

How to fix place for bottomnavigation menu item in their place in CoordinatorLayout android?

I'm working on a BottomNavigation for android.

I created a ViewBehavior class for my BottomNavigation.There are four item in my BottomNavigation. The problem I'm facing is it took larger space for the selected item. When I select any item it took more place then others with a little animation. But I need to fix those item's place.Each

What can be the possible solution?

My BottomNavigationViewBehavior Class is:

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

private int height;

@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
    height = child.getHeight();
    return super.onLayoutChild(parent, child, layoutDirection);
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    if (dyConsumed > 0) {
        slideDown(child);
    } else if (dyConsumed < 0) {
        slideUp(child);
    }
}

private void slideUp(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(0).setDuration(200);
}

private void slideDown(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(height).setDuration(200);
}

}

And here is my 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottomNavigation"
    android:layout_alignParentTop="true"/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigation"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_gravity="bottom"
    android:background="@color/colorAccent"
    app:menu="@menu/navigation" />

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

My current situation

选定的第4个项目比其他项目占据更多位置

Ok. Finally I got my answer from Here .I just need to make another class or method as BottomNavigationViewHelper.

Use this class

class BottomNavigationViewHelper {

static void removeShiftMode(BottomNavigationView view) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
    try {
        Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
        shiftingMode.setAccessible(true);
        shiftingMode.setBoolean(menuView, false);
        shiftingMode.setAccessible(false);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
            item.setShiftingMode(false);
            // set once again checked value, so view will be updated
            item.setChecked(item.getItemData().isChecked());
        }
    } catch (NoSuchFieldException e) {
        Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field");
    } catch (IllegalAccessException e) {
        Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode");
    }
}
}

And use this class like this:

BottomNavigationView bottomNavigationView =(BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

Original answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM