Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.devone.haute.ui.widget;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.os.Build;
- import android.support.design.widget.AppBarLayout;
- import android.support.design.widget.CoordinatorLayout;
- import android.support.v4.view.ViewCompat;
- import android.util.AttributeSet;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewParent;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.devone.haute.R;
- import com.devone.haute.util.Utils;
- public class UndoTaskLayout extends LinearLayout {
- ViewGroup parent;
- int duration;
- TextView messageView;
- Button actionView;
- private boolean firstLayout = true;
- public UndoTaskLayout(Context context) {
- this(context, null);
- }
- public UndoTaskLayout(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public UndoTaskLayout(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context, attrs);
- }
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public UndoTaskLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- init(context, attrs);
- }
- private void init(Context context, AttributeSet attrs) {
- // LayoutInflater.from(context).inflate(R.layout.undo_layout, this, true);
- }
- public static UndoTaskLayout make(View view, int i) {
- ViewGroup parent = findSuitableParent(view);
- UndoTaskLayout undoTaskLayout = (UndoTaskLayout) LayoutInflater
- .from(parent.getContext()).inflate(R.layout.undo_layout, parent, false);
- undoTaskLayout.parent = parent;
- undoTaskLayout.duration = i;
- ViewCompat.setElevation(undoTaskLayout, Utils.dpToPx(view.getContext(), 2));
- return undoTaskLayout;
- }
- private static ViewGroup findSuitableParent(View view) {
- ViewGroup fallback = null;
- do {
- if (view instanceof CoordinatorLayout) {
- // We've found a CoordinatorLayout, use it
- return (ViewGroup) view;
- } else if (view instanceof FrameLayout) {
- if (view.getId() == android.R.id.content) {
- // If we've hit the decor content view, then we didn't find a CoL in the
- // hierarchy, so use it.
- return (ViewGroup) view;
- } else {
- // It's not the content view but we'll use it as our fallback
- fallback = (ViewGroup) view;
- }
- }
- if (view != null) {
- // Else, we will loop and crawl up the view hierarchy and try to find a parent
- final ViewParent parent = view.getParent();
- view = parent instanceof View ? (View) parent : null;
- }
- } while (view != null);
- // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
- return fallback;
- }
- @Override
- protected void onFinishInflate() {
- super.onFinishInflate();
- messageView = (TextView) findViewById(R.id.undo_text);
- actionView = (Button) findViewById(R.id.undo_action);
- }
- public UndoTaskLayout setAction(final View.OnClickListener listener) {
- actionView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- listener.onClick(v);
- dismiss();
- }
- });
- return this;
- }
- public void dismiss() {
- parent.removeView(this);
- }
- public void show() {
- if (this.getParent() == null) {
- final ViewGroup.LayoutParams lp = this.getLayoutParams();
- if (lp instanceof CoordinatorLayout.LayoutParams) {
- // If our LayoutParams are from a CoordinatorLayout, we'll setup our Behavior
- AppBarLayout appBarLayout = findFirstAppBarInParent(parent);
- Behavior behavior = new Behavior();
- ((CoordinatorLayout.LayoutParams) lp).setAnchorId(appBarLayout.getId());
- ((CoordinatorLayout.LayoutParams) lp).gravity = Gravity.BOTTOM;
- ((CoordinatorLayout.LayoutParams) lp).anchorGravity = Gravity.BOTTOM;
- ((CoordinatorLayout.LayoutParams) lp).setBehavior(behavior);
- }
- parent.addView(this);
- }
- if (ViewCompat.isLaidOut(this)) {
- // TODO animate
- } else firstLayout = true;
- }
- private AppBarLayout findFirstAppBarInParent(ViewGroup parent) {
- AppBarLayout appBar = null;
- for (int i = 0; i < parent.getChildCount(); i++) { //attach listener to all buttons
- View v = parent.getChildAt(i);
- if (v instanceof AppBarLayout) appBar = (AppBarLayout) v;
- }
- if (appBar != null) return appBar;
- throw new IllegalStateException("Cant find an AppBar");
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- if (firstLayout) {
- // TODO animate
- firstLayout = false;
- }
- }
- @Override
- protected void onAttachedToWindow() {
- super.onAttachedToWindow();
- }
- @Override
- protected void onDetachedFromWindow() {
- super.onDetachedFromWindow();
- }
- class Behavior extends CoordinatorLayout.Behavior<UndoTaskLayout> {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement