如何用代码更改BottomNavigationView的选定选项卡?

时间:2016-12-24 22:20:34

标签: android android-design-library bottomnavigationview

无论如何都要从" com.android.support更改BottomNavigationView的选定标签:design"带代码的库?

    <android.support.design.widget.BottomNavigationView
        xmlns:design="http://schema.android.com/apk/res/android.support.design"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/bottom_bar"
        design:menu="@menu/bottom_bar"/>

3 个答案:

答案 0 :(得分:0)

看起来我们需要使用updateMenuView()方法更新BottomNavigaitonPresenter的状态。带反射的示例代码;

                        bottomNavigationView.getMenu().getItem(3).setChecked(false);
                    bottomNavigationView.getMenu().getItem(2).setChecked(false);
                    bottomNavigationView.getMenu().getItem(1).setChecked(false);
                    bottomNavigationView.getMenu().getItem(0).setChecked(true);

                    try {
                        final Field mPresenter = bottomNavigationView.getClass().getField("mPresenter");
                        mPresenter.setAccessible(true);

                        final BottomNavigationPresenter presenter = (BottomNavigationPresenter) mPresenter.get(bottomNavigationView);
                        presenter.updateMenuView(true);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }

答案 1 :(得分:0)

这是我的位置BottomNavigationView的代码。

/**
 * Created by tysheng
 * Date: 2016/12/31 12:24.
 * Email: tyshengsx@gmail.com
 */

public class PositionBottomNavigationView extends BottomNavigationView implements BottomNavigationView.OnNavigationItemSelectedListener {
    private SparseIntArray mMenuIds;
    private int currentPosition;
    private onPositionSelectedListener mListener;

    public PositionBottomNavigationView(Context context) {
        this(context, null, 0);
    }

    public PositionBottomNavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PositionBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMenuIds = new SparseIntArray();
        setOnNavigationItemSelectedListener(this);
    }

    /**
     * ATTENTION: register the ids first.
     *
     * @param ids menu ids by order, like R.id.menu_0,R.id.menu_1...
     */
    public void registerIds(int... ids) {
        mMenuIds.clear();
        for (int i = 0; i < ids.length; i++) {
            mMenuIds.put(i, ids[i]);
        }
    }

    public void setOnPositionSelectedListener(onPositionSelectedListener listener) {
        mListener = listener;
    }

    public interface onPositionSelectedListener {
        void onPositionSelected(int position);
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        setPositionChecked();
        if (mListener != null)
            mListener.onPositionSelected(currentPosition);
    }

    private void setPositionChecked() {
        Menu menu = getMenu();
        if (menu.getItem(currentPosition).isChecked()) {
            return;
        }
        for (int i = 0; i < mMenuIds.size(); i++) {
            if (mMenuIds.get(i) != mMenuIds.get(currentPosition)) {
                menu.getItem(i).setChecked(false);
            } else {
                menu.getItem(i).setChecked(true);
            }
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int pos = mMenuIds.indexOfValue(item.getItemId());
        if (pos != -1) {
            setCurrentPosition(pos);
        }
        return true;
    }
}

答案 2 :(得分:0)

这是正确的方法:

要以编程方式单击您需要使用的BottomNavigationBar项目:

View view = bottomNavigationView.findViewById(R.id.action_item);
view.performClick();

这不仅可以通过编程方式单击BottomNavigationBar,还可以正确排列标签。