Java Code Examples for android.support.v4.view.ViewCompat#setOnApplyWindowInsetsListener()

The following examples show how to use android.support.v4.view.ViewCompat#setOnApplyWindowInsetsListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScrimInsetsFrameLayout.java    From SublimeNavigationView with Apache License 2.0 8 votes vote down vote up
public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mTempRect = new Rect();
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.SnvScrimInsetsFrameLayout,
            defStyleAttr,
            R.style.SnvScrimInsetsFrameLayout);
    this.mInsetForeground = a.getDrawable(R.styleable.SnvScrimInsetsFrameLayout_snvInsetForeground);
    a.recycle();
    this.setWillNotDraw(true);
    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                    if (null == ScrimInsetsFrameLayout.this.mInsets) {
                        ScrimInsetsFrameLayout.this.mInsets = new Rect();
                    }

                    ScrimInsetsFrameLayout.this.mInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
                    ScrimInsetsFrameLayout.this.setWillNotDraw(ScrimInsetsFrameLayout.this.mInsets.isEmpty() || ScrimInsetsFrameLayout.this.mInsetForeground == null);
                    ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this);
                    return insets.consumeSystemWindowInsets();
                }
            });
}
 
Example 2
Source File: SystemBarMetrics.java    From ShaderEditor with MIT License 6 votes vote down vote up
private static void setWindowInsets(final View mainLayout,
		final Rect windowInsets) {
	ViewCompat.setOnApplyWindowInsetsListener(mainLayout, new OnApplyWindowInsetsListener() {
		@Override
		public WindowInsetsCompat onApplyWindowInsets(View v,
				WindowInsetsCompat insets) {
			if (insets.hasSystemWindowInsets()) {
				int left = insets.getSystemWindowInsetLeft();
				int top = insets.getSystemWindowInsetTop();
				int right = insets.getSystemWindowInsetRight();
				int bottom = insets.getSystemWindowInsetBottom();
				mainLayout.setPadding(left, top, right, bottom);
				if (windowInsets != null) {
					windowInsets.set(left, top, right, bottom);
				}
			}
			return insets.consumeSystemWindowInsets();
		}
	});
}
 
Example 3
Source File: InsetsRecyclerView.java    From mr-mantou-android with GNU General Public License v3.0 6 votes vote down vote up
public InsetsRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.InsetsRecyclerView);
    padding = a.getDimensionPixelOffset(R.styleable.InsetsRecyclerView_padding, 0);
    a.recycle();

    setPadding(padding, padding, padding, padding);

    ViewCompat.setOnApplyWindowInsetsListener(this, (v, insets) -> {
        final int l = padding + insets.getSystemWindowInsetLeft();
        final int r = padding + insets.getSystemWindowInsetRight();
        final int b = padding + insets.getSystemWindowInsetBottom();
        setPadding(l, padding, r, b);
        return insets.consumeSystemWindowInsets();
    });
}
 
Example 4
Source File: ParamsActivity.java    From ImmersionBar with Apache License 2.0 6 votes vote down vote up
@SuppressLint("SetTextI18n")
@Override
protected void setListener() {
    super.setListener();
    mBtnStatus.setOnClickListener(v -> {
        if (!mIsHideStatusBar) {
            ImmersionBar.hideStatusBar(getWindow());
            mIsHideStatusBar = true;
        } else {
            ImmersionBar.showStatusBar(getWindow());
            mIsHideStatusBar = false;
        }
    });

    ViewCompat.setOnApplyWindowInsetsListener(mTvInsets, (view, windowInsetsCompat) -> {
        mTvInsets.setText(getText(getTitle(mTvInsets) + windowInsetsCompat.getSystemWindowInsetTop()));
        return windowInsetsCompat.consumeSystemWindowInsets();
    });
}
 
Example 5
Source File: StatusBarCompatLollipop.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static void setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor) {
    Window window = activity.getWindow();
    window.clearFlags(NTLMConstants.FLAG_UNIDENTIFIED_9);
    window.addFlags(Integer.MIN_VALUE);
    window.setStatusBarColor(0);
    window.getDecorView().setSystemUiVisibility(0);
    View mChildView = ((ViewGroup) window.findViewById(16908290)).getChildAt(0);
    if (mChildView != null) {
        ViewCompat.setOnApplyWindowInsetsListener(mChildView, new OnApplyWindowInsetsListener() {
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                return insets;
            }
        });
        ViewCompat.setFitsSystemWindows(mChildView, true);
        ViewCompat.requestApplyInsets(mChildView);
    }
    ((View) appBarLayout.getParent()).setFitsSystemWindows(true);
    appBarLayout.setFitsSystemWindows(true);
    collapsingToolbarLayout.setFitsSystemWindows(true);
    collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(true);
    toolbar.setFitsSystemWindows(false);
    collapsingToolbarLayout.setStatusBarScrimColor(statusColor);
}
 
Example 6
Source File: InsetsFrameLayout.java    From scene with Apache License 2.0 6 votes vote down vote up
public InsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v,
                                                              WindowInsetsCompat insets) {
                    setPadding(insets.getSystemWindowInsetLeft(),
                            insets.getSystemWindowInsetTop(),
                            insets.getSystemWindowInsetRight(),
                            0);
                    ViewCompat.postInvalidateOnAnimation(InsetsFrameLayout.this);
                    return insets;
                }
            });
}
 
Example 7
Source File: InsetsToolbar.java    From mr-mantou-android with GNU General Public License v3.0 5 votes vote down vote up
public InsetsToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    ViewCompat.setOnApplyWindowInsetsListener(this, (v, insets) -> {
        final int l = insets.getSystemWindowInsetLeft();
        final int t = insets.getSystemWindowInsetTop();
        final int r = insets.getSystemWindowInsetRight();
        setPadding(l, t, r, 0);
        return insets.consumeSystemWindowInsets();
    });
}
 
Example 8
Source File: Floaty.java    From Floaty with Apache License 2.0 5 votes vote down vote up
private Floaty(ViewGroup parent, View content) {
    if (parent == null) {
        throw new IllegalArgumentException("Transient bottom bar must have non-null parent");
    }
    if (content == null) {
        throw new IllegalArgumentException("Transient bottom bar must have non-null content");
    }

    targetParent = parent;
    context = parent.getContext();
    view = (FloatyContentLayout) content;

    stage = Stage.of(targetParent);

    ViewCompat.setAccessibilityLiveRegion(stage, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
    ViewCompat.setImportantForAccessibility(stage, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    // Make sure that we fit system windows and have a listener to apply any insets
    stage.setFitsSystemWindows(true);
    ViewCompat.setOnApplyWindowInsetsListener(stage,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                    // Copy over the bottom inset as padding so that we're displayed
                    // above the navigation bar
                    v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(),
                            insets.getSystemWindowInsetBottom());
                    return insets;
                }
            });

    accessibilityManager =
            (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);

    scene = new Scene(stage, view);
}
 
Example 9
Source File: SweetSnackbar.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
public SnackbarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SnackbarLayout);
    mMaxWidth = a.getDimensionPixelSize(R.styleable.SnackbarLayout_android_maxWidth, -1);
    mMaxInlineActionWidth = a.getDimensionPixelSize(
            R.styleable.SnackbarLayout_maxActionInlineWidth, -1);
    if (a.hasValue(R.styleable.SnackbarLayout_elevation)) {
        ViewCompat.setElevation(this, a.getDimensionPixelSize(
                R.styleable.SnackbarLayout_elevation, 0));
    }
    a.recycle();

    setClickable(true);

    // Now inflate our content. We need to do this manually rather than using an <include>
    // in the layout since older versions of the Android do not inflate includes with
    // the correct Context.
    LayoutInflater.from(context).inflate(com.jet.sweettips.R.layout.sweet_layout_snackbar_include, this);

    ViewCompat.setAccessibilityLiveRegion(this,
            ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
    ViewCompat.setImportantForAccessibility(this,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    // Make sure that we fit system windows and have a listener to apply any insets
    ViewCompat.setFitsSystemWindows(this, true);
    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            // Copy over the bottom inset as padding so that we're displayed above the
            // navigation bar
            v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
                    v.getPaddingRight(), insets.getSystemWindowInsetBottom());
            return insets;
        }
    });
}
 
Example 10
Source File: InsetsToolbar.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
public InsetsToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            final int l = insets.getSystemWindowInsetLeft();
            final int t = insets.getSystemWindowInsetTop();
            final int r = insets.getSystemWindowInsetRight();
            setPadding(l, t, r, 0);
            return insets.consumeSystemWindowInsets();
        }
    });
}
 
Example 11
Source File: SweetSnackbar.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
public SnackbarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SnackbarLayout);
    mMaxWidth = a.getDimensionPixelSize(R.styleable.SnackbarLayout_android_maxWidth, -1);
    mMaxInlineActionWidth = a.getDimensionPixelSize(
            R.styleable.SnackbarLayout_maxActionInlineWidth, -1);
    if (a.hasValue(R.styleable.SnackbarLayout_elevation)) {
        ViewCompat.setElevation(this, a.getDimensionPixelSize(
                R.styleable.SnackbarLayout_elevation, 0));
    }
    a.recycle();

    setClickable(true);

    // Now inflate our content. We need to do this manually rather than using an <include>
    // in the layout since older versions of the Android do not inflate includes with
    // the correct Context.
    LayoutInflater.from(context).inflate(com.jet.sweettips.R.layout.sweet_layout_snackbar_include, this);

    ViewCompat.setAccessibilityLiveRegion(this,
            ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
    ViewCompat.setImportantForAccessibility(this,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    // Make sure that we fit system windows and have a listener to apply any insets
    ViewCompat.setFitsSystemWindows(this, true);
    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            // Copy over the bottom inset as padding so that we're displayed above the
            // navigation bar
            v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
                    v.getPaddingRight(), insets.getSystemWindowInsetBottom());
            return insets;
        }
    });
}
 
Example 12
Source File: AppBarLayout.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
public AppBarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(VERTICAL);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppBarLayout,
            0, R.style.Widget_Ticwear_AppBarLayout);
    mTargetElevation = a.getDimensionPixelSize(R.styleable.AppBarLayout_android_elevation, 0);
    setBackgroundDrawable(a.getDrawable(R.styleable.AppBarLayout_android_background));
    if (a.hasValue(R.styleable.AppBarLayout_tic_expanded)) {
        setExpanded(a.getBoolean(R.styleable.AppBarLayout_tic_expanded, false));
    }
    a.recycle();

    // Use the bounds view outline provider so that we cast a shadow, even without a background
    setOutlineProvider(ViewOutlineProvider.BOUNDS);

    mListeners = new ArrayList<>();

    ViewCompat.setElevation(this, mTargetElevation);

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v,
                                                              WindowInsetsCompat insets) {
                    if (isShown()) {
                        setWindowInsets(insets);
                        return insets.consumeSystemWindowInsets();
                    } else {
                        return insets;
                    }
                }
            });
}
 
Example 13
Source File: CoordinatorLayout.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
private void setupForWindowInsets() {
    if (ViewCompat.getFitsSystemWindows(this)) {
        // First apply the insets listener
        ViewCompat.setOnApplyWindowInsetsListener(this, new ApplyInsetsListener());
        // Now set the sys ui flags to enable us to lay out in the window insets
        setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
}
 
Example 14
Source File: DirectionalViewpager.java    From fangzhuishushenqi with Apache License 2.0 4 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);
    mTopEdge = new EdgeEffectCompat(context);
    mBottomEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                              final WindowInsetsCompat originalInsets) {
                    // First let the ViewPager itself try and consume them...
                    final WindowInsetsCompat applied =
                            ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the ViewPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since ViewPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 15
Source File: YViewPager.java    From YViewPagerDemo with Apache License 2.0 4 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = configuration.getScaledPagingTouchSlop();
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);
    mTopEdge = new EdgeEffectCompat(context);
    mBottomEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new YViewPager.MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                              final WindowInsetsCompat originalInsets) {
                    // First let the ViewPager itself try and consume them...
                    final WindowInsetsCompat applied =
                            ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the ViewPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since ViewPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 16
Source File: DirectionalViewpager.java    From BookReader with Apache License 2.0 4 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);
    mTopEdge = new EdgeEffectCompat(context);
    mBottomEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                              final WindowInsetsCompat originalInsets) {
                    // First let the ViewPager itself try and consume them...
                    final WindowInsetsCompat applied =
                            ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the ViewPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since ViewPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 17
Source File: DirectionalViewpager.java    From ankihelper with GNU General Public License v3.0 4 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);
    mTopEdge = new EdgeEffectCompat(context);
    mBottomEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support
                    .v4.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat
                        onApplyWindowInsets(final View v,
                                    final WindowInsetsCompat originalInsets) {
                    // First let the ViewPager itself try and consume them...
                    final WindowInsetsCompat applied =
                                ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the ViewPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since ViewPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left
                                = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 18
Source File: GpCollapsingToolbar.java    From GpCollapsingToolbar with Apache License 2.0 4 votes vote down vote up
@SuppressLint("PrivateResource")
public GpCollapsingToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    ThemeUtils.checkAppCompatTheme(context);

    mCollapsingTextHelper = new CollapsingTextHelper(this);
    mCollapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.GpCollapsingToolbarLayout, defStyleAttr,
            R.style.Widget_Design_CollapsingToolbar);

    mCollapsingTextHelper.setExpandedTextGravity(
            a.getInt(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleGravity,
            GravityCompat.START | Gravity.BOTTOM));

    mCollapsingTextHelper.setCollapsedTextGravity(
            a.getInt(R.styleable.GpCollapsingToolbarLayout_gp_collapsedTitleGravity,
                    GravityCompat.START | Gravity.CENTER_VERTICAL));

    mExpandedMarginStart = mExpandedMarginTop = mExpandedMarginEnd = mExpandedMarginBottom =
            a.getDimensionPixelSize(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMargin, 0);

    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginStart)) {
        mExpandedMarginStart = a.getDimensionPixelSize(
                R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginStart, 0);
    }
    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginEnd)) {
        mExpandedMarginEnd = a.getDimensionPixelSize(
                R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginEnd, 0);
    }
    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginTop)) {
        mExpandedMarginTop = a.getDimensionPixelSize(
                R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginTop, 0);
    }
    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginBottom)) {
        mExpandedMarginBottom = a.getDimensionPixelSize(
                R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleMarginBottom, 0);
    }

    mCollapsingTitleEnabled = a.getBoolean(R.styleable.GpCollapsingToolbarLayout_gp_titleEnabled, true);
    mGooglePlayBehaviour = a.getBoolean(R.styleable.GpCollapsingToolbarLayout_gp_marketStyledBehaviour, false);
    setTitle(a.getText(R.styleable.GpCollapsingToolbarLayout_gp_title));

    // First load the default text appearances
    mCollapsingTextHelper.setExpandedTextAppearance(R.style.TextAppearance_Design_CollapsingToolbar_Expanded);
    mCollapsingTextHelper.setCollapsedTextAppearance(R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);

    // Now overlay any custom text appearances
    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleTextAppearance)) {
        mCollapsingTextHelper.setExpandedTextAppearance(
                a.getResourceId(R.styleable.GpCollapsingToolbarLayout_gp_expandedTitleTextAppearance, 0));
    }
    if (a.hasValue(R.styleable.GpCollapsingToolbarLayout_gp_collapsedTitleTextAppearance)) {
        mCollapsingTextHelper.setCollapsedTextAppearance(
                a.getResourceId(R.styleable.GpCollapsingToolbarLayout_gp_collapsedTitleTextAppearance, 0));
    }

    mScrimVisibleHeightTrigger = a.getDimensionPixelSize(
            R.styleable.GpCollapsingToolbarLayout_gp_scrimVisibleHeightTrigger, 0);

    mScrimAnimationDuration = a.getInt(
            R.styleable.GpCollapsingToolbarLayout_gp_scrimAnimationDuration,
            DEFAULT_SCRIM_ANIMATION_DURATION);

    setContentScrim(a.getDrawable(R.styleable.GpCollapsingToolbarLayout_gp_contentScrim));
    setStatusBarScrim(a.getDrawable(R.styleable.GpCollapsingToolbarLayout_gp_statusBarScrim));

    mToolbarId = a.getResourceId(R.styleable.GpCollapsingToolbarLayout_gp_toolbarId, -1);

    a.recycle();

    setWillNotDraw(false);

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                    return setWindowInsets(insets);
                }
            });
}
 
Example 19
Source File: CollapsingToolbarLayout.java    From ticdesign with Apache License 2.0 4 votes vote down vote up
public CollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mCollapsingTextHelper = new CollapsingTextHelper(this);
    mCollapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CollapsingToolbarLayout, defStyleAttr,
            R.style.Widget_Ticwear_CollapsingToolbar);

    mCollapsingTextHelper.setExpandedTextGravity(
            a.getInt(R.styleable.CollapsingToolbarLayout_tic_expandedTitleGravity,
                    GravityCompat.START | Gravity.BOTTOM));
    mCollapsingTextHelper.setCollapsedTextGravity(
            a.getInt(R.styleable.CollapsingToolbarLayout_tic_collapsedTitleGravity,
                    GravityCompat.START | Gravity.CENTER_VERTICAL));

    mExpandedMarginStart = mExpandedMarginTop = mExpandedMarginEnd = mExpandedMarginBottom =
            a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMargin, 0);

    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginStart)) {
        mExpandedMarginStart = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginStart, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginEnd)) {
        mExpandedMarginEnd = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginEnd, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginTop)) {
        mExpandedMarginTop = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginTop, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginBottom)) {
        mExpandedMarginBottom = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginBottom, 0);
    }

    mCollapsingTitleEnabled = a.getBoolean(
            R.styleable.CollapsingToolbarLayout_tic_titleEnabled, true);
    setTitle(a.getText(R.styleable.CollapsingToolbarLayout_android_title));

    // First load the default text appearances
    mCollapsingTextHelper.setExpandedTextAppearance(
            R.style.TextAppearance_Ticwear_CollapsingToolbar_Expanded);
    mCollapsingTextHelper.setCollapsedTextAppearance(
            R.style.TextAppearance_Ticwear_TitleBar_Title);

    // Now overlay any custom text appearances
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleTextAppearance)) {
        mCollapsingTextHelper.setExpandedTextAppearance(
                a.getResourceId(
                        R.styleable.CollapsingToolbarLayout_tic_expandedTitleTextAppearance, 0));
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_collapsedTitleTextAppearance)) {
        mCollapsingTextHelper.setCollapsedTextAppearance(
                a.getResourceId(
                        R.styleable.CollapsingToolbarLayout_tic_collapsedTitleTextAppearance, 0));
    }

    setContentScrim(a.getDrawable(R.styleable.CollapsingToolbarLayout_tic_contentScrim));
    setStatusBarScrim(a.getDrawable(R.styleable.CollapsingToolbarLayout_tic_statusBarScrim));

    mToolbarId = a.getResourceId(R.styleable.CollapsingToolbarLayout_tic_toolbarId, -1);

    a.recycle();

    setWillNotDraw(false);

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v,
                        WindowInsetsCompat insets) {
                    if (isShown()) {
                        mLastInsets = insets;
                        requestLayout();
                        return insets.consumeSystemWindowInsets();
                    } else {
                        return insets;
                    }
                }
            });
}
 
Example 20
Source File: StretchingLayout.java    From ticdesign with Apache License 2.0 4 votes vote down vote up
public StretchingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mCollapsingTextHelper = new CollapsingTextHelper(this);
    mCollapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.CollapsingToolbarLayout, defStyleAttr,
            R.style.Widget_Ticwear_CollapsingToolbar);

    mCollapsingTextHelper.setExpandedTextGravity(
            a.getInt(R.styleable.CollapsingToolbarLayout_tic_expandedTitleGravity,
                    GravityCompat.START | Gravity.BOTTOM));
    mCollapsingTextHelper.setCollapsedTextGravity(
            a.getInt(R.styleable.CollapsingToolbarLayout_tic_collapsedTitleGravity,
                    GravityCompat.START | Gravity.CENTER_VERTICAL));

    mExpandedMarginStart = mExpandedMarginTop = mExpandedMarginEnd = mExpandedMarginBottom =
            a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMargin, 0);

    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginStart)) {
        mExpandedMarginStart = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginStart, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginEnd)) {
        mExpandedMarginEnd = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginEnd, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginTop)) {
        mExpandedMarginTop = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginTop, 0);
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginBottom)) {
        mExpandedMarginBottom = a.getDimensionPixelSize(
                R.styleable.CollapsingToolbarLayout_tic_expandedTitleMarginBottom, 0);
    }

    mCollapsingTitleEnabled = a.getBoolean(
            R.styleable.CollapsingToolbarLayout_tic_titleEnabled, true);
    setTitle(a.getText(R.styleable.CollapsingToolbarLayout_android_title));

    // First load the default text appearances
    mCollapsingTextHelper.setExpandedTextAppearance(
            R.style.TextAppearance_Ticwear_CollapsingToolbar_Expanded);
    mCollapsingTextHelper.setCollapsedTextAppearance(
            R.style.TextAppearance_Ticwear_TitleBar_Title);

    // Now overlay any custom text appearances
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_expandedTitleTextAppearance)) {
        mCollapsingTextHelper.setExpandedTextAppearance(
                a.getResourceId(
                        R.styleable.CollapsingToolbarLayout_tic_expandedTitleTextAppearance, 0));
    }
    if (a.hasValue(R.styleable.CollapsingToolbarLayout_tic_collapsedTitleTextAppearance)) {
        mCollapsingTextHelper.setCollapsedTextAppearance(
                a.getResourceId(
                        R.styleable.CollapsingToolbarLayout_tic_collapsedTitleTextAppearance, 0));
    }

    a.recycle();

    setWillNotDraw(false);

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View v,
                        WindowInsetsCompat insets) {
                    if (isShown()) {
                        mLastInsets = insets;
                        requestLayout();
                        return insets.consumeSystemWindowInsets();
                    } else {
                        return insets;
                    }
                }
            });
}