コード例 #1
0
ファイル: Snackbar.cs プロジェクト: pacificIT/SnackbarSharp
 public Snackbar Type(SnackbarType type)
 {
     _type = type;
     return(this);
 }
コード例 #2
0
ファイル: Snackbar.cs プロジェクト: pacificIT/SnackbarSharp
        private FrameLayout.LayoutParams Init(Context parent)
        {
            var layout = (SnackbarLayout)LayoutInflater.From(parent).Inflate(Resource.Layout.sb__template, this, true);
            var res    = Resources;

            _offset = 0;
            var scale = res.DisplayMetrics.Density;

            FrameLayout.LayoutParams layoutParams = null;
            if (res.GetBoolean(Resource.Boolean.sb__is_phone))
            {
                layout.SetMinimumHeight(DpToPx(_type.GetMinHeight(), scale));
                layout.SetMaxHeight(DpToPx(_type.GetMaxHeight(), scale));
                layout.SetBackgroundColor(_color);
                layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
                                                            ViewGroup.LayoutParams.WrapContent);
            }
            else
            {
                // Tablet/desktop
                _type = SnackbarType.SingleLine;                 // Force single-line
                layout.SetMinimumWidth(res.GetDimensionPixelSize(Resource.Dimension.sb__min_width));
                layout.SetMaxWidth(res.GetDimensionPixelSize(Resource.Dimension.sb__max_width));
                layout.SetBackgroundResource(Resource.Drawable.sb__bg);
                var bg = (GradientDrawable)layout.Background;
                bg.SetColor(_color);
                layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                                                            DpToPx(_type.GetMaxHeight(), scale))
                {
                    LeftMargin   = _offset,
                    BottomMargin = _offset
                };
            }

            layoutParams.Gravity = GravityFlags.Bottom;
            var snackbarText = (TextView)layout.FindViewById(Resource.Id.sb__text);

            snackbarText.Text = _text;
            snackbarText.SetTextColor(_textColor);
            snackbarText.SetMaxLines(_type.GetMaxLines());

            var snackbarAction = (TextView)layout.FindViewById(Resource.Id.sb__action);

            if (!TextUtils.IsEmpty(_actionLabel))
            {
                RequestLayout();
                snackbarAction.Text = _actionLabel;
                snackbarAction.SetTextColor(_actionColor);

                snackbarAction.Click += (sender, args) =>
                {
                    if (_actionClickListener != null)
                    {
                        _actionClickListener.OnActionClicked(this);
                    }
                    if (_shouldDismissOnActionClicked)
                    {
                        Dismiss();
                    }
                };
                snackbarAction.SetMaxLines(_type.GetMaxLines());
            }
            else
            {
                snackbarAction.Visibility = ViewStates.Gone;
            }

            Clickable = true;

            Func <object, bool> canDismiss = token => true;

            Action <View, object> onDismiss = (view, token) =>
            {
                if (view != null)
                {
                    Dismiss(false);
                }
            };

            Action <bool> pauseTimer = shouldPause =>
            {
                if (IsIndefiniteDuration())
                {
                    return;
                }
                if (shouldPause)
                {
                    RemoveCallbacks(_dismissRunnable);

                    _snackbarFinish = DateTime.Now.Millisecond;
                }
                else
                {
                    _timeRemaining -= (_snackbarFinish -
                                       _snackbarStart);
                    StartTimer(_timeRemaining);
                }
            };

            var swipeDismissTouchListener = new SwipeDismissTouchListener(this, null, canDismiss, onDismiss, pauseTimer);

            if (_canSwipeToDismiss && res.GetBoolean(Resource.Boolean.sb__is_swipeable))
            {
                SetOnTouchListener(swipeDismissTouchListener);
            }

            return(layoutParams);
        }