android.support.design.widget.CoordinatorLayout Java Examples

The following examples show how to use android.support.design.widget.CoordinatorLayout. 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: MainActivity.java    From AndroidWallet with GNU General Public License v3.0 7 votes vote down vote up
private void showAccountNotExistDialog(BaseInvokeModel baseInvokeModel, BaseInfo baseInfo) {
    bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
    DialogAuthorAccountNotExistBinding binding = DataBindingUtil.inflate(LayoutInflater.from(Utils.getContext()), R.layout.dialog_author_account_not_exist, null, false);
    bottomSheetDialog.setContentView(binding.getRoot());
    // 设置dialog 完全显示
    View parent = (View) binding.getRoot().getParent();
    BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
    binding.getRoot().measure(0, 0);
    behavior.setPeekHeight(binding.getRoot().getMeasuredHeight());
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
    params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    parent.setLayoutParams(params);
    bottomSheetDialog.setCanceledOnTouchOutside(false);
    final ConfrimDialogViewModel confrimDialogViewModel = new ConfrimDialogViewModel(getApplication());
    confrimDialogViewModel.setBaseInfo(baseInvokeModel, baseInfo);
    binding.setViewModel(confrimDialogViewModel);
    bottomSheetDialog.show();
}
 
Example #2
Source File: PercentageViewBehaviorTest.java    From simple-view-behavior with MIT License 6 votes vote down vote up
@Test
public void dependsOnHeight() {
    SimpleViewBehavior behavior = new SimpleViewBehavior.Builder()
            .dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_HEIGHT)
            .targetValue(100)
            .targetX(100)
            .targetY(200)
            .build();
    CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
    params.setBehavior(behavior);
    secondView.setLayoutParams(params);

    // resize first view's height
    CoordinatorLayout.LayoutParams resizeParams = new CoordinatorLayout.LayoutParams(firstView.getLayoutParams());
    resizeParams.height = 100;
    firstView.setLayoutParams(resizeParams);
    coordinatorLayout.requestLayout();
    assertEquals(1.0f, secondView.getCurrentPercent());
}
 
Example #3
Source File: BottomBehavior.java    From v9porn with MIT License 6 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, LinearLayout child, int layoutDirection) {
    parent.onLayoutChild(child, layoutDirection);
    if (isFirst) {
        isFirst = false;
        if (oLy == 0) {
            oLy = child.getY() + child.getHeight();
        }
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            if (view instanceof AppBarLayout) {
                height = view.getMeasuredHeight();
                break;
            }
        }

        child.setY(oLy);
    }

    return true;
}
 
Example #4
Source File: UcNewsHeaderPagerBehavior.java    From UcMainPagerDemo with Apache License 2.0 6 votes vote down vote up
private void settle(CoordinatorLayout parent, final View child) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "settle: ");
    }
    if (mFlingRunnable != null) {
        child.removeCallbacks(mFlingRunnable);
        mFlingRunnable = null;
    }
    mFlingRunnable = new FlingRunnable(parent, child);
    if (child.getTranslationY() < getHeaderOffsetRange() / 3.0f) {
        mFlingRunnable.scrollToClosed(DURATION_SHORT);
    } else {
        mFlingRunnable.scrollToOpen(DURATION_SHORT);
    }

}
 
Example #5
Source File: BaseBehavior.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
private void initScrollTarget(final CoordinatorLayout coordinatorLayout, final AppBarLayout child) {
  Utils.log("initScrollTarget | %b", vScrollTarget != null);
  if (vScrollTarget != null) {
    long tag = getViewTag(vScrollTarget, true);
    if (!mScrollTargets.contains(tag)) {
      mScrollTargets.add(tag);
      OnScrollListener listener = new OnScrollListener() {

        @Override
        public void onScrollChanged(View view, int x, int y, int dx, int dy, boolean accuracy) {
          if (view == vScrollTarget) {
            BaseBehavior.this.onScrollChanged(coordinatorLayout, child, view, y, dy, accuracy);
          }
        }
      };
      if (vScrollTarget instanceof NestedScrollView) {
        ObservableNestedScrollView.newInstance((NestedScrollView) vScrollTarget, mOverrideOnScrollListener, listener);
      } else if (vScrollTarget instanceof RecyclerView) {
        ObservableRecyclerView.newInstance((RecyclerView) vScrollTarget, listener);
      }
    }
  }
}
 
Example #6
Source File: CafeBar.java    From cafebar with Apache License 2.0 6 votes vote down vote up
public void show() {
    mSnackBar.show();

    if (mBuilder.mSwipeToDismiss) return;

    if (mSnackBar.getView().getLayoutParams() instanceof CoordinatorLayout.LayoutParams) {
        mSnackBar.getView().getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                mSnackBar.getView().getViewTreeObserver().removeOnPreDrawListener(this);
                ((CoordinatorLayout.LayoutParams) mSnackBar.getView().getLayoutParams()).setBehavior(null);
                return true;
            }
        });
    }
}
 
Example #7
Source File: CircleImageInUsercBehavior.java    From ToDoList with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, CircleImageView child, View dependency) {
    //初始化一些基础参数
    init(parent, child, dependency);
    //计算比例
    if (child.getY() <= 0) return false;
    float percent = (child.getY() - mToolBarHeight) / (mStartAvatarY - mToolBarHeight);

    if (percent < 0) {
        percent = 0;
    }
    if (this.percent == percent || percent > 1) return true;
    this.percent = percent;
   //设置头像的大小
    ViewCompat.setScaleX(child, percent);
    ViewCompat.setScaleY(child, percent);

    return false;
}
 
Example #8
Source File: DisableableAppBarLayout.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
    super.setLayoutParams(params);
    if (params instanceof CoordinatorLayout.LayoutParams) {
        ((CoordinatorLayout.LayoutParams) params).setBehavior(mBehavior);
    }
}
 
Example #9
Source File: FooterBehavior.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FooterLayout child,
                                      View dependency) {
    if (dependency instanceof Snackbar.SnackbarLayout) {
        updateTranslationForSnackbar(parent, child);
    }
    return false;
}
 
Example #10
Source File: TopSheetBehavior.java    From AndroidTopSheet with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target,
                                float velocityX, float velocityY) {
    return target == mNestedScrollingChildRef.get() &&
            (mState != STATE_EXPANDED ||
                    super.onNestedPreFling(coordinatorLayout, child, target,
                            velocityX, velocityY));
}
 
Example #11
Source File: FABSnackbarBehavior.java    From FABsMenu with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    if (dependency instanceof Snackbar.SnackbarLayout) {
        this.updateFabTranslationForSnackbar(parent, child, dependency);
    }
    return false;
}
 
Example #12
Source File: SnackbarUtils.java    From PicKing with Apache License 2.0 5 votes vote down vote up
/**
 * 设置Snackbar显示的位置,当Snackbar和CoordinatorLayout组合使用的时候
 *
 * @param gravity
 */
public SnackbarUtils gravityCoordinatorLayout(int gravity) {
    if (getSnackbar() != null) {
        CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(getSnackbar().getView().getLayoutParams().width, getSnackbar().getView().getLayoutParams().height);
        params.gravity = gravity;
        getSnackbar().getView().setLayoutParams(params);
    }
    return this;
}
 
Example #13
Source File: MyUtils.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 5 votes vote down vote up
public static void setToolbarOffset(CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, int offset) {
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) layoutParams.getBehavior();
    if (behavior != null) {
        behavior.setTopAndBottomOffset(offset);
        behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, null, 0, 1, new int[2]);
    }
}
 
Example #14
Source File: WeiboHeaderPagerBehavior.java    From CoordinatorLayoutExample with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target,
                             float velocityX, float velocityY, boolean consumed) {
    Log.i(TAG, "onNestedFling: velocityY=" + velocityY);
    return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY,
            consumed);

}
 
Example #15
Source File: BottomSheetBehaviorGoogleMapsLike.java    From Nibo with MIT License 5 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
    // First let the parent lay it out
    if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
        if (ViewCompat.getFitsSystemWindows(parent) &&
                !ViewCompat.getFitsSystemWindows(child)) {
            ViewCompat.setFitsSystemWindows(child, true);
        }
        parent.onLayoutChild(child, layoutDirection);
    }
    // Offset the bottom sheet
    mParentHeight = parent.getHeight();
    mMinOffset = Math.max(0, mParentHeight - child.getHeight());
    mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);

    /**
     * New behavior
     */
    if (mState == STATE_ANCHOR_POINT) {
        ViewCompat.offsetTopAndBottom(child, mAnchorPoint);
    } else if (mState == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, mMinOffset);
    } else if (mHideable && mState == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, mParentHeight);
    } else if (mState == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, mMaxOffset);
    }
    if (mViewDragHelper == null) {
        mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
    }
    mViewRef = new WeakReference<>(child);
    mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
    return true;
}
 
Example #16
Source File: FABSnackbarBehavior.java    From FABsMenu with Apache License 2.0 5 votes vote down vote up
/**
 * Find the {@code translation Y} value for any child Snackbar components.
 *
 * @return 0.0F if there are no Snackbar components found, otherwise returns the min offset that
 * the FAB component should be animated.
 */
private float getFabTranslationYForSnackbar(CoordinatorLayout parent, View fab) {
    float minOffset = 0.0F;
    final List<View> dependencies = parent.getDependencies(fab);

    for (View view : dependencies) {
        if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) {
            minOffset = Math.min(minOffset, view.getTranslationY() - (float) view.getHeight());
        }
    }

    return minOffset;
}
 
Example #17
Source File: TipActivity.java    From AndroidDonate with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tip);

    Intent intent = getIntent();
    if (intent == null) {
        finish();
        return;
    }
    String title = intent.getStringExtra("title");
    String tip = intent.getStringExtra("tip");
    int bgRes = intent.getIntExtra("res", R.drawable.p1);

    ActionBar supportActionBar = getSupportActionBar();
    if (supportActionBar != null) {
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setTitle(title);
    }

    coordinatorLayout = ((CoordinatorLayout) findViewById(R.id.coordinator));
    imageView = ((ImageView) findViewById(R.id.image));


    imageView.setImageResource(bgRes);
    showSnackBar(tip);
}
 
Example #18
Source File: MultiChoicesCircleButtonBehavior.java    From MultiChoicesCircleButton with MIT License 5 votes vote down vote up
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, MultiChoicesCircleButton child,
                           View target, int dxConsumed, int dyConsumed,
                           int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    if (dyConsumed > 0 || dyUnconsumed > 0) {
        child.hide();
    } else if (dyConsumed < 0 || dyUnconsumed < 0) {
        child.show();
    }
}
 
Example #19
Source File: FABScrollBehavior.java    From Expense-Tracker-App with MIT License 5 votes vote down vote up
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
        FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
    String a = directTargetChild.getClass().getSimpleName();
    //excluding Statistics case and History for FAB behavior
    if (target instanceof NestedScrollView && target.getTag() != null) {
        if (target.getTag().toString().equalsIgnoreCase(ExpenseTrackerApp.getContext().getString(R.string.statistics)) || target.getTag().toString().equalsIgnoreCase(ExpenseTrackerApp.getContext().getString(R.string.history))) {
            return false;
        }
    }
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
        super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
 
Example #20
Source File: ScrollAwareFABBehavior2.java    From MaoWanAndoidClient with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {

    if (target instanceof RecyclerView) {
        return true;
    }
    return false;
}
 
Example #21
Source File: BottomSheetBuilder.java    From BottomSheetBuilder with Apache License 2.0 5 votes vote down vote up
public View createView() {

        if (mMenu == null && mAdapterBuilder.getItems().isEmpty()) {
            throw new IllegalStateException("You need to provide at least one Menu " +
                    "or an item with addItem");
        }

        if (mCoordinatorLayout == null) {
            throw new IllegalStateException("You need to provide a coordinatorLayout" +
                    "so the view can be placed on it");
        }

        View sheet = mAdapterBuilder.createView(mTitleTextColor, mBackgroundDrawable,
                mBackgroundColor, mDividerBackground, mItemTextColor, mItemBackground,
                mIconTintColor, mItemClickListener);

        ViewCompat.setElevation(sheet, mContext.getResources()
                .getDimensionPixelSize(R.dimen.bottomsheet_elevation));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            sheet.findViewById(R.id.fakeShadow).setVisibility(View.GONE);
        }

        CoordinatorLayout.LayoutParams layoutParams
                = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT,
                CoordinatorLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        layoutParams.setBehavior(new BottomSheetBehavior());

        if (mContext.getResources().getBoolean(R.bool.tablet_landscape)) {
            layoutParams.width = mContext.getResources()
                    .getDimensionPixelSize(R.dimen.bottomsheet_width);
        }

        mCoordinatorLayout.addView(sheet, layoutParams);
        mCoordinatorLayout.postInvalidate();
        return sheet;
    }
 
Example #22
Source File: UcNewsTabBehavior.java    From CoordinatorLayoutExample with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "onDependentViewChanged: dependency.getTranslationY():"+dependency.getTranslationY());
    }
    offsetChildAsNeeded(parent, child, dependency);
    return false;
}
 
Example #23
Source File: GroupHeaderBehavior.java    From CoordinatorLayoutExample with Apache License 2.0 5 votes vote down vote up
private void handleActionUp(CoordinatorLayout parent, final View child) {
    if (mFlingRunnable != null) {
        child.removeCallbacks(mFlingRunnable);
        mFlingRunnable = null;
    }

    mFlingRunnable = new FlingRunnable(parent, child);

    //notice  is a negative number
    float targetHeight = getHeaderOffsetRange() / 8.0f;
    float childTranslationY = child.getTranslationY();

    logD(TAG, "handleActionUp: childTranslationY=" + childTranslationY + "targetHeight=" +
            targetHeight + " mCurState= " + mCurState);

    if (mCurState == STATE_OPENED) {

        if (childTranslationY < targetHeight) {
            mFlingRunnable.scrollToClosed(DURATION_SHORT);
        } else {
            mFlingRunnable.scrollToOpen(DURATION_SHORT);
        }

    } else if (mCurState == STATE_CLOSED) {

        float percent = 1 - Math.abs(childTranslationY * 1.0f / getHeaderOffsetRange());
        childTranslationY = getHeaderOffsetRange() - childTranslationY;
        logD(TAG, "handleActionUp: childTranslationY=" + childTranslationY + "percent=" +
                percent);

        if (percent < 0.15) {
            mFlingRunnable.scrollToClosed(DURATION_SHORT);
        } else {
            mFlingRunnable.scrollToOpen(DURATION_SHORT);
        }

    }


}
 
Example #24
Source File: BottomNavigationViewBehavior.java    From playa with MIT License 5 votes vote down vote up
@Override
public boolean onStartNestedScroll(
        @NonNull CoordinatorLayout coordinatorLayout,
        @NonNull BottomNavigationView child,
        @NonNull View directTargetChild,
        @NonNull View target, int axes, int type) {
    return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
 
Example #25
Source File: BottomVerticalScrollBehavior.java    From JD-Test with Apache License 2.0 5 votes vote down vote up
private Snackbar.SnackbarLayout getSnackBarInstance(CoordinatorLayout parent, V child) {
    final List<View> dependencies = parent.getDependencies(child);
    for (int i = 0, z = dependencies.size(); i < z; i++) {
        final View view = dependencies.get(i);
        if (view instanceof Snackbar.SnackbarLayout) {
            return (Snackbar.SnackbarLayout) view;
        }
    }
    return null;
}
 
Example #26
Source File: MainActivity.java    From triviums with MIT License 5 votes vote down vote up
private void initBinding() {
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    //Attaching bottom sheet behaviour - hide/show on scroll
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
            binding.navigation.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationBehaviour());
}
 
Example #27
Source File: ViewPagerBottomSheetBehavior.java    From FabulousFilter with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
    if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
        ViewCompat.setFitsSystemWindows(child, true);
    }
    int savedTop = child.getTop();
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);
    // Offset the bottom sheet
    mParentHeight = parent.getHeight();
    int peekHeight;
    if (mPeekHeightAuto) {
        if (mPeekHeightMin == 0) {
            mPeekHeightMin = parent.getResources().getDimensionPixelSize(
                    R.dimen.design_bottom_sheet_peek_height_min);
        }
        peekHeight = Math.max(mPeekHeightMin, mParentHeight - parent.getWidth() * 9 / 16);
    } else {
        peekHeight = mPeekHeight;
    }
    mMinOffset = Math.max(0, mParentHeight - child.getHeight());
    mMaxOffset = Math.max(mParentHeight - peekHeight, mMinOffset);
    if (mState == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, mMinOffset);
    } else if (mHideable && mState == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, mParentHeight);
    } else if (mState == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, mMaxOffset);
    } else if (mState == STATE_DRAGGING || mState == STATE_SETTLING) {
        ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
    }
    if (mViewDragHelper == null) {
        mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
    }
    mViewRef = new WeakReference<>(child);
    mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
    return true;
}
 
Example #28
Source File: FixAppBarLayoutBehavior.java    From CameraBlur with Apache License 2.0 5 votes vote down vote up
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                           int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, type);
    stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
}
 
Example #29
Source File: AppBarLayoutBehavior.java    From YCRefreshView with Apache License 2.0 5 votes vote down vote up
/**
 * 是否拦截触摸事件
 * @param parent                        CoordinatorLayout
 * @param child                         AppBarLayout
 * @param ev                            ev
 * @return
 */
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
    LogUtil.d(TAG, "onInterceptTouchEvent:" + child.getTotalScrollRange());
    shouldBlockNestedScroll = isFlinging;
    switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            //手指触摸屏幕的时候停止fling事件
            stopAppbarLayoutFling(child);
            break;
        default:
            break;
    }
    return super.onInterceptTouchEvent(parent, child, ev);
}
 
Example #30
Source File: WhlBehavior.java    From customview-samples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
    mShouldBlockNestedScroll = false;
    if (mIsFlinging) {
        mShouldBlockNestedScroll = true;
    }
    return super.onInterceptTouchEvent(parent, child, ev);
}