Advertisement
Guest User

Untitled

a guest
May 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package com.devone.haute.ui.widget;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.os.Build;
  6. import android.support.design.widget.AppBarLayout;
  7. import android.support.design.widget.CoordinatorLayout;
  8. import android.support.v4.view.ViewCompat;
  9. import android.util.AttributeSet;
  10. import android.view.Gravity;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.view.ViewParent;
  15. import android.widget.Button;
  16. import android.widget.FrameLayout;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19.  
  20. import com.devone.haute.R;
  21. import com.devone.haute.util.Utils;
  22.  
  23. public class UndoTaskLayout extends LinearLayout {
  24.  
  25. ViewGroup parent;
  26. int duration;
  27.  
  28. TextView messageView;
  29. Button actionView;
  30.  
  31. private boolean firstLayout = true;
  32.  
  33.  
  34. public UndoTaskLayout(Context context) {
  35. this(context, null);
  36. }
  37.  
  38. public UndoTaskLayout(Context context, AttributeSet attrs) {
  39. this(context, attrs, 0);
  40. }
  41.  
  42. public UndoTaskLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  43. super(context, attrs, defStyleAttr);
  44.  
  45. init(context, attrs);
  46. }
  47.  
  48. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  49. public UndoTaskLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  50. super(context, attrs, defStyleAttr, defStyleRes);
  51.  
  52. init(context, attrs);
  53. }
  54.  
  55. private void init(Context context, AttributeSet attrs) {
  56.  
  57. // LayoutInflater.from(context).inflate(R.layout.undo_layout, this, true);
  58. }
  59.  
  60.  
  61.  
  62. public static UndoTaskLayout make(View view, int i) {
  63.  
  64. ViewGroup parent = findSuitableParent(view);
  65.  
  66. UndoTaskLayout undoTaskLayout = (UndoTaskLayout) LayoutInflater
  67. .from(parent.getContext()).inflate(R.layout.undo_layout, parent, false);
  68.  
  69. undoTaskLayout.parent = parent;
  70. undoTaskLayout.duration = i;
  71.  
  72. ViewCompat.setElevation(undoTaskLayout, Utils.dpToPx(view.getContext(), 2));
  73.  
  74. return undoTaskLayout;
  75. }
  76.  
  77. private static ViewGroup findSuitableParent(View view) {
  78.  
  79. ViewGroup fallback = null;
  80.  
  81. do {
  82. if (view instanceof CoordinatorLayout) {
  83.  
  84. // We've found a CoordinatorLayout, use it
  85. return (ViewGroup) view;
  86. } else if (view instanceof FrameLayout) {
  87.  
  88. if (view.getId() == android.R.id.content) {
  89. // If we've hit the decor content view, then we didn't find a CoL in the
  90. // hierarchy, so use it.
  91. return (ViewGroup) view;
  92. } else {
  93. // It's not the content view but we'll use it as our fallback
  94. fallback = (ViewGroup) view;
  95. }
  96. }
  97.  
  98. if (view != null) {
  99.  
  100. // Else, we will loop and crawl up the view hierarchy and try to find a parent
  101. final ViewParent parent = view.getParent();
  102. view = parent instanceof View ? (View) parent : null;
  103. }
  104.  
  105. } while (view != null);
  106.  
  107. // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
  108. return fallback;
  109. }
  110.  
  111. @Override
  112. protected void onFinishInflate() {
  113. super.onFinishInflate();
  114. messageView = (TextView) findViewById(R.id.undo_text);
  115. actionView = (Button) findViewById(R.id.undo_action);
  116. }
  117.  
  118.  
  119. public UndoTaskLayout setAction(final View.OnClickListener listener) {
  120. actionView.setOnClickListener(new OnClickListener() {
  121. @Override
  122. public void onClick(View v) {
  123.  
  124. listener.onClick(v);
  125.  
  126. dismiss();
  127. }
  128. });
  129.  
  130. return this;
  131. }
  132.  
  133. public void dismiss() {
  134. parent.removeView(this);
  135. }
  136.  
  137. public void show() {
  138.  
  139. if (this.getParent() == null) {
  140.  
  141. final ViewGroup.LayoutParams lp = this.getLayoutParams();
  142.  
  143. if (lp instanceof CoordinatorLayout.LayoutParams) {
  144. // If our LayoutParams are from a CoordinatorLayout, we'll setup our Behavior
  145.  
  146. AppBarLayout appBarLayout = findFirstAppBarInParent(parent);
  147. Behavior behavior = new Behavior();
  148.  
  149. ((CoordinatorLayout.LayoutParams) lp).setAnchorId(appBarLayout.getId());
  150. ((CoordinatorLayout.LayoutParams) lp).gravity = Gravity.BOTTOM;
  151. ((CoordinatorLayout.LayoutParams) lp).anchorGravity = Gravity.BOTTOM;
  152. ((CoordinatorLayout.LayoutParams) lp).setBehavior(behavior);
  153. }
  154.  
  155. parent.addView(this);
  156. }
  157.  
  158. if (ViewCompat.isLaidOut(this)) {
  159.  
  160. // TODO animate
  161. } else firstLayout = true;
  162. }
  163.  
  164. private AppBarLayout findFirstAppBarInParent(ViewGroup parent) {
  165.  
  166. AppBarLayout appBar = null;
  167.  
  168. for (int i = 0; i < parent.getChildCount(); i++) { //attach listener to all buttons
  169.  
  170. View v = parent.getChildAt(i);
  171. if (v instanceof AppBarLayout) appBar = (AppBarLayout) v;
  172. }
  173.  
  174. if (appBar != null) return appBar;
  175.  
  176. throw new IllegalStateException("Cant find an AppBar");
  177. }
  178.  
  179. @Override
  180. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  181. super.onLayout(changed, l, t, r, b);
  182.  
  183. if (firstLayout) {
  184.  
  185. // TODO animate
  186. firstLayout = false;
  187. }
  188. }
  189.  
  190. @Override
  191. protected void onAttachedToWindow() {
  192. super.onAttachedToWindow();
  193. }
  194.  
  195. @Override
  196. protected void onDetachedFromWindow() {
  197. super.onDetachedFromWindow();
  198. }
  199.  
  200. class Behavior extends CoordinatorLayout.Behavior<UndoTaskLayout> {
  201.  
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement