-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathItemMenuLayout.java
More file actions
130 lines (108 loc) · 3.61 KB
/
Copy pathItemMenuLayout.java
File metadata and controls
130 lines (108 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.qpython.qpy.program;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
public class ItemMenuLayout extends FrameLayout {
private static final String TAG = "ItemMenuLayout";
private static ItemMenuLayout sOpenMenuView = null;
private View mMenu;
private OnClickListener mListener;
private int mDuration = 300;
public ItemMenuLayout(Context context) {
super(context);
}
public ItemMenuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ItemMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() != 2) {
throw new IllegalStateException("Must be have two child view");
}
mMenu = getChildAt(1);
LayoutParams params = (LayoutParams) mMenu.getLayoutParams();
params.gravity = Gravity.RIGHT;
mMenu.setLayoutParams(params);
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onClick(v);
} else {
if (!isMenuVisible()) {
showMenu();
}
}
}
});
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mMenu.setVisibility(GONE);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (MotionEvent.ACTION_UP == ev.getAction()) {
if (sOpenMenuView != null) {
sOpenMenuView.hideMenu();
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (sOpenMenuView != null) {
sOpenMenuView.hideMenu();
;
}
return super.onTouchEvent(event);
}
@Override
public void setOnClickListener(OnClickListener l) {
mListener = l;
}
private boolean isMenuVisible() {
return mMenu.getVisibility() == View.VISIBLE;
}
private void showMenu() {
sOpenMenuView = this;
mMenu.setVisibility(VISIBLE);
Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
anim.setDuration(mDuration);
mMenu.startAnimation(anim);
}
private void hideMenu() {
sOpenMenuView = null;
Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
anim.setDuration(mDuration);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mMenu.setVisibility(GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mMenu.startAnimation(anim);
}
}