Monday, 27 August 2018

Highlighted Navigation Drawer issue in Marshmallow and above

I have a navigation drawer, in tablet view when

android:windowSoftInputMode="adjustResize"

and soft keyboard is visible all navigation drawer will be highlighted. I see this issue in android 6 and above, setting windowSoftInputMode to adjustPan resolve highlight issue but is not suitable for my situation.

Layout is somtion like this:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
...
    <com.x.common.ui.CollapsingNavigationDrawer
        android:id="@+id/sliding_pane_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="locale"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:navigation_collapse_mode_width="@dimen/partial_pane_width"
        app:navigation_menu_id="@menu/activity_correspondence_drawer"
        app:navigation_view_id="@+id/nav_view"
        app:show_staff="true">

        <FrameLayout
            android:id="@+id/activity_correspondence__content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.x.common.ui.CollapsingNavigationDrawer>
....
</android.support.design.widget.CoordinatorLayout>

CollapsingNavigationDrawer class:

public class CollapsingNavigationDrawer extends SlidingPaneLayout {

    private boolean mShowStaff;
    private int mNavigationMenuId;
    private int mNavigationId;
    private int mCollapseModeWidth;
    private int mNavigationViewWidth;

    public CollapsingNavigationDrawer(Context context) {

        super(context);
    }

    public CollapsingNavigationDrawer(Context context, AttributeSet attrs) {

        super(context, attrs);
        initResources(attrs);
        init();
    }

    public CollapsingNavigationDrawer(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        initResources(attrs);
        init();
    }

    private void init() {

        View navigationView = getNavigationView();
        LayoutParams layoutParams = new LayoutParams(mNavigationViewWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(navigationView, layoutParams);
    }

    @NonNull
    private View getNavigationView() {

        NavigationView navigationView = (NavigationView) View.inflate(getContext(), R.layout.navigation_view, null);

        navigationView.setBackgroundColor(Color.WHITE);
        navigationView.setFitsSystemWindows(false);
        navigationView.setId(mNavigationId);
        mNavigationViewWidth = getResources().getDimensionPixelSize(R.dimen.sliding_pane_navigation_view_width);
        View headerView = View.inflate(getContext(), R.layout.navigation_drawer_header, null);
        View rootView = headerView.getRootView();
        rootView.setPadding(rootView.getPaddingLeft(), rootView.getPaddingTop() + getResources().getDimensionPixelSize(R.dimen.sliding_pane_navigation_margin_top), rootView.getPaddingRight(), rootView.getPaddingBottom());
        navigationView.addHeaderView(headerView);
        navigationView.inflateMenu(mNavigationMenuId);
        // FrameLayout is used to fix the problem of not collapsing navigationView in post lollipop devices.
        FrameLayout frameLayout = new FrameLayout(getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(0, -1 * getResources().getDimensionPixelSize(R.dimen.sliding_pane_navigation_margin_top), 0, 0);
        frameLayout.addView(navigationView, layoutParams);
        frameLayout.setId(R.id.navigation_view_container);
        return frameLayout;
    }

    private void initResources(AttributeSet attributeSet) {

        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attributeSet, R.styleable.CollapsingNavigationDrawer, 0, 0);
        try {
            mNavigationId = typedArray.getResourceId(R.styleable.CollapsingNavigationDrawer_navigation_view_id, 0);
            mShowStaff = typedArray.getBoolean(R.styleable.CollapsingNavigationDrawer_show_staff, true);
            mNavigationMenuId = typedArray.getResourceId(R.styleable.CollapsingNavigationDrawer_navigation_menu_id, 0);
            mCollapseModeWidth = typedArray.getDimensionPixelSize(R.styleable.CollapsingNavigationDrawer_navigation_collapse_mode_width, 0);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {

        if (child.getId() != R.id.navigation_view_container) {
            MarginLayoutParams marginLayoutParams = new MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            marginLayoutParams.setMarginStart(mCollapseModeWidth);
            super.addView(getChildViewWithDivider(child, params), index, marginLayoutParams);
            return;
        }
        super.addView(child, index, params);
    }

    @NonNull
    private LinearLayout getChildViewWithDivider(View child, ViewGroup.LayoutParams params) {

        LinearLayout root = new LinearLayout(getContext());
        root.setOrientation(LinearLayout.HORIZONTAL);
        View divider = View.inflate(getContext(), R.layout.sliding_pane_divider_view, null);
        divider.setId(R.id.navigation_view_divider);
        LayoutParams lp = new LayoutParams(getResources().getDimensionPixelSize(R.dimen.divider_width), ViewGroup.LayoutParams.MATCH_PARENT);
        //((LayoutParams) params).setMarginStart(mCollapseModeWidth);
        root.addView(divider, lp);
        root.addView(child, params);
        return root;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        return false;
    }

    public void showDivider() {

        View divider = findViewById(R.id.navigation_view_divider);
        if (divider != null) {
            divider.setVisibility(VISIBLE);
        }
    }

    public void hideDivider() {

        View divider = findViewById(R.id.navigation_view_divider);
        if (divider != null) {
            divider.setVisibility(GONE);
        }
    }
}

Preview



from Highlighted Navigation Drawer issue in Marshmallow and above

No comments:

Post a Comment