-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextMenuDialogFragment.cs
140 lines (118 loc) · 4.6 KB
/
ContextMenuDialogFragment.cs
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
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Yalantis.Contextmenu.Lib;
using Com.Yalantis.Contextmenu.Lib.Interfaces;
using Android.Support.V4.App;
namespace ContextMenuXamarinAndroid
{
class ContextMenuDialogFragment : DialogFragment, IOnItemClickListener, IOnItemLongClickListener
{
public static String TAG = typeof(ContextMenuDialogFragment).Name;
private static String BUNDLE_MENU_PARAMS = "BUNDLE_MENU_PARAMS";
private LinearLayout mWrapperButtons;
private LinearLayout mWrapperText;
private MenuAdapter mDropDownMenuAdapter;
private IOnMenuItemClickListener mItemClickListener;
private IOnMenuItemLongClickListener mItemLongClickListener;
private MenuParams mMenuParams;
public static int STYLE_NO_FRAME = 2;
public static ContextMenuDialogFragment NewInstance(MenuParams menuParams)
{
ContextMenuDialogFragment fragment = new ContextMenuDialogFragment();
Bundle args = new Bundle();
args.PutParcelable(BUNDLE_MENU_PARAMS, menuParams);
fragment.Arguments = args;
return fragment;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetStyle(StyleNoFrame, Resource.Style.MenuFragmentStyle);
if (Arguments != null)
{
mMenuParams = (MenuParams)Arguments.GetParcelable(BUNDLE_MENU_PARAMS);
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.fragment_menu, container, false);
rootView.SetFitsSystemWindows(mMenuParams.FitsSystemWindow);
((ViewGroup)rootView).SetClipToPadding(mMenuParams.ClipToPadding);
initViews(rootView);
Dialog.Window.ClearFlags(WindowManagerFlags.DimBehind);
initDropDownMenuAdapter();
Handler h = new Handler();
Action myAction = () =>
{
// your code that you want to delay here
mDropDownMenuAdapter.MenuToggle();
};
h.PostDelayed(myAction, mMenuParams.AnimationDelay);
if (mMenuParams.ClosableOutside)
{
rootView.FindViewById<RelativeLayout>(Resource.Id.root).Click += (sender, args) =>
{
if (IsAdded)
{
DismissAllowingStateLoss();
}
};
}
return rootView;
}
private void initViews(View view)
{
mWrapperButtons = view.FindViewById<LinearLayout>(Resource.Id.wrapper_buttons);
mWrapperText = view.FindViewById<LinearLayout>(Resource.Id.wrapper_text);
}
private void initDropDownMenuAdapter()
{
mDropDownMenuAdapter = new MenuAdapter(Activity, mWrapperButtons, mWrapperText,
mMenuParams.MenuObjects, mMenuParams.ActionBarSize);
mDropDownMenuAdapter.SetOnItemClickListener(this);
mDropDownMenuAdapter.SetOnItemLongClickListener(this);
mDropDownMenuAdapter.SetAnimationDuration(mMenuParams.AnimationDuration);
}
private void close()
{
Handler h = new Handler();
Action myAction = () =>
{
DismissAllowingStateLoss();
};
h.PostDelayed(myAction, mMenuParams.AnimationDelay);
}
public void setItemLongClickListener(IOnMenuItemLongClickListener itemLongClickListener)
{
this.mItemLongClickListener = itemLongClickListener;
}
public void setItemClickListener(IOnMenuItemClickListener itemClickListener)
{
this.mItemClickListener = itemClickListener;
}
void IOnItemClickListener.OnClick(View v)
{
if (mItemClickListener != null)
{
mItemClickListener.OnMenuItemClick(v, ((ViewGroup)v.Parent).IndexOfChild(v));
}
close();
}
void IOnItemLongClickListener.OnLongClick(View v)
{
if (mItemLongClickListener != null)
{
mItemLongClickListener.OnMenuItemLongClick(v, ((ViewGroup)v.Parent).IndexOfChild(v));
}
close();
}
}
}