mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Fix BottomNavigationMenu item selection via Menu object
am: 69557a3a03 Change-Id: Iaab10e75354d53096958671d18bd47dc07dfdb28 GitOrigin-RevId=b4da44d7478295076c41ad68ffbced62f0e3b8d4 PiperOrigin-RevId: 141509777
This commit is contained in:
parent
29d3d012d5
commit
8f5bfa5881
@ -16,6 +16,8 @@
|
||||
|
||||
package android.support.design.internal;
|
||||
|
||||
import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.content.res.Resources;
|
||||
@ -36,8 +38,6 @@ import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
@ -131,8 +131,6 @@ public class BottomNavigationItemView extends FrameLayout implements MenuView.It
|
||||
|
||||
@Override
|
||||
public void setChecked(boolean checked) {
|
||||
mItemData.setChecked(checked);
|
||||
|
||||
ViewCompat.setPivotX(mLargeLabel, mLargeLabel.getWidth() / 2);
|
||||
ViewCompat.setPivotY(mLargeLabel, mLargeLabel.getBaseline());
|
||||
ViewCompat.setPivotX(mSmallLabel, mSmallLabel.getWidth() / 2);
|
||||
|
||||
@ -16,14 +16,15 @@
|
||||
|
||||
package android.support.design.internal;
|
||||
|
||||
import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.RestrictTo;
|
||||
import android.support.v7.view.menu.MenuBuilder;
|
||||
import android.support.v7.view.menu.MenuItemImpl;
|
||||
import android.view.MenuItem;
|
||||
import android.view.SubMenu;
|
||||
|
||||
import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
@ -47,6 +48,12 @@ public final class BottomNavigationMenu extends MenuBuilder {
|
||||
"Maximum number of items supported by BottomNavigationView is " + MAX_ITEM_COUNT
|
||||
+ ". Limit can be checked with BottomNavigationView#getMaxItemCount()");
|
||||
}
|
||||
return super.addInternal(group, id, categoryOrder, title);
|
||||
stopDispatchingItemsChanged();
|
||||
final MenuItem item = super.addInternal(group, id, categoryOrder, title);
|
||||
if (item instanceof MenuItemImpl) {
|
||||
((MenuItemImpl) item).setExclusiveCheckable(true);
|
||||
}
|
||||
startDispatchingItemsChanged();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,6 +287,9 @@ public class BottomNavigationMenuView extends ViewGroup implements MenuView {
|
||||
}
|
||||
for (int i = 0; i < menuSize; i++) {
|
||||
mPresenter.setUpdateSuspended(true);
|
||||
if (mMenu.getItem(i).isChecked()) {
|
||||
mActiveButton = i;
|
||||
}
|
||||
mButtons[i].initialize((MenuItemImpl) mMenu.getItem(i), 0);
|
||||
mPresenter.setUpdateSuspended(false);
|
||||
}
|
||||
@ -297,10 +300,7 @@ public class BottomNavigationMenuView extends ViewGroup implements MenuView {
|
||||
|
||||
mAnimationHelper.beginDelayedTransition(this);
|
||||
|
||||
mPresenter.setUpdateSuspended(true);
|
||||
mButtons[mActiveButton].setChecked(false);
|
||||
mButtons[newButton].setChecked(true);
|
||||
mPresenter.setUpdateSuspended(false);
|
||||
mMenu.getItem(newButton).setChecked(true);
|
||||
|
||||
mActiveButton = newButton;
|
||||
}
|
||||
|
||||
@ -47,7 +47,6 @@ import android.support.test.filters.SmallTest;
|
||||
import android.support.v4.content.res.ResourcesCompat;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -212,4 +211,27 @@ public class BottomNavigationViewTest
|
||||
onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_people)))).check(
|
||||
matches(TestUtilsMatchers.drawable(blueFill, allowedComponentVariance)));
|
||||
}
|
||||
|
||||
@UiThreadTest
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testItemChecking() throws Throwable {
|
||||
final Menu menu = mBottomNavigation.getMenu();
|
||||
assertTrue(menu.getItem(0).isChecked());
|
||||
checkAndVerifyExclusiveItem(menu, R.id.destination_home);
|
||||
checkAndVerifyExclusiveItem(menu, R.id.destination_profile);
|
||||
checkAndVerifyExclusiveItem(menu, R.id.destination_people);
|
||||
}
|
||||
|
||||
private void checkAndVerifyExclusiveItem(final Menu menu, final int id) throws Throwable {
|
||||
menu.findItem(id).setChecked(true);
|
||||
for (int i = 0; i < menu.size(); i++) {
|
||||
final MenuItem item = menu.getItem(i);
|
||||
if (item.getItemId() == id) {
|
||||
assertTrue(item.isChecked());
|
||||
} else {
|
||||
assertFalse(item.isChecked());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user