Пример #1
0
        /**
         * Display your menu. Not we require a view from the parent.  This is so we can get
         * a window token.  It doesn't matter which view on the parent is passed in.
         * @param View v
         * @return void
         */
        public void Show(View v, GravityFlags screenLocation, bool enableSelectedIndicator)
        {
            if (table != null && table.Visibility == ViewStates.Gone)
            {
                Reshow();
                return;
            }

            if (mIsShowing)
            {
                return;
            }

            int itemCount = mMenuItems.Count;

            if (itemCount < 1)
            {
                return;
            }

            if (imageViewList.Count > 0)
            {
                imageViewList.Clear();
            }
            if (badgeTextViewList.Count > 0)
            {
                badgeTextViewList.Clear();
            }
            if (titleViewList.Count > 0)
            {
                titleViewList.Clear();
            }
            if (indicatorList.Count > 0)
            {
                indicatorList.Clear();
            }

            mView       = mLayoutInflater.Inflate(UIHelper.GetResource(UIHelper.Layout, "custom_menu"), null);
            tableParent = mView.FindViewById(UIHelper.GetResource(UIHelper.Id, "table_parent")) as LinearLayout;

            if (mView == null)
            {
                return;
            }

            var tableId = UIHelper.GetResource(UIHelper.Id, "custom_menu_table");

            table = (TableLayout)mView.FindViewById(tableId);
            if (table == null)
            {
                return;
            }

            var windowManager = (mContext as Activity).WindowManager;

            table.RemoveAllViews();

            TableRow row     = null;
            TextView tv      = null;
            TextView tvBadge = null;
            TextView selected_item_indicator = null;

            ImageView iv = null;

            row = new TableRow(mContext);
            row.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
            for (int j = 0; j < mItemsCount; j++)
            {
                if (j >= itemCount)
                {
                    break;
                }

                CustomMenuItem cmi        = mMenuItems.ElementAt(j);
                View           itemLayout = mLayoutInflater.Inflate(UIHelper.GetLayoutResource("custom_menu_item"), null);
                tv = (TextView)itemLayout.FindViewById(UIHelper.GetId("custom_menu_item_caption"));
                tv.SetTextColor(this.textColor);

                if (String.IsNullOrEmpty(cmi.Caption))
                {
                    tv.Visibility = ViewStates.Gone;
                }
                else
                {
                    tv.Text       = cmi.Caption;
                    tv.Visibility = ViewStates.Visible;
                }
                titleViewList.Add(tv);

                iv = (ImageView)itemLayout.FindViewById(UIHelper.GetId("custom_menu_item_icon"));

                if (cmi.ImageResourceId > 0)
                {
                    iv.SetImageResource(cmi.ImageResourceId);
                }

                iv.Tag = cmi.Id;
                imageViewList.Add(iv);

                selected_item_indicator = itemLayout.FindViewById(UIHelper.GetId("selected_item_indicator")) as TextView;
                if (enableSelectedIndicator)
                {
                    selected_item_indicator.Visibility = ViewStates.Visible;
                }

                indicatorList.Add(selected_item_indicator);

                tvBadge = itemLayout.FindViewById(UIHelper.GetId("custom_badge")) as TextView;

                if (String.IsNullOrEmpty(cmi.Badge))
                {
                    tvBadge.Visibility = ViewStates.Gone;
                }
                else
                {
                    int badgeCount = int.Parse(cmi.Badge);
                    if (badgeCount > 0)
                    {
                        tvBadge.Text = badgeCount.ToString();
                        //tvBadge.SetBackgroundDrawable(drawbg(tvBadge));
                        tvBadge.Visibility = ViewStates.Visible;

                        //if (Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.JellyBean)
                        //{
                        //    //TODO: Fix x location of badge
                        //    tvBadge.SetX(iv.GetX() + iv.Drawable.IntrinsicWidth);
                        //}
                    }
                }
                badgeTextViewList.Add(tvBadge);

                itemLayout.Click += delegate(object sender, EventArgs e)
                {
                    if (EnableClickingTab)
                    {
                        selectedIndex = cmi.Id;
                        mListener.MenuItemSelectedEvent(cmi, imageViewList, titleViewList);
                    }
                };

                row.AddView(itemLayout);
            }

            if (selectedIndex >= 0)
            {
                if (mMenuItems.ElementAt(selectedIndex).ActiveImageId > 0)
                {
                    imageViewList.ElementAt(selectedIndex).SetImageResource(mMenuItems.ElementAt(selectedIndex).ActiveImageId);
                }

                titleViewList.ElementAt(selectedIndex).SetTextColor(selectedTextColor);
                indicatorList.ElementAt(selectedIndex).SetBackgroundColor(selectedTextColor);

                //var ivParent = (RelativeLayout)imageViewList[selectedIndex].Parent.Parent;
                //ivParent.Background = mContext.Resources.GetDrawable(UIHelper.GetDrawableResource("my_menu_item_pressed"));
            }

            table.AddView(row);

            mIsShowing = true;
        }