2

BottomNavigationView の選択したタブを「com.android.support: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"/>
4

3 に答える 3

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;
    }
}
于 2016-12-31T05:11:23.930 に答える
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();
                    }
于 2016-12-25T11:15:46.260 に答える