Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Customise MasterDetailPage cells

$
0
0

Hello everybody,

I'm working on customize the background and textcolor of an item selected in the menu in a masterDetailPage and I'm running out of solutions.
I want to change the text color of the cell selected and remove the color of the previous item (going back to the original state).

So I have a MenuPage :

 public class MenuPage : CustomPage
    {
        public ListView Menu { get; set; }
        private MenuItem selected { get; set; }


        public MenuPage ()
        {

            Icon = "settings.png";
            Title = "Menu"; // The Title property must be set.

            BackgroundColor = Color.FromHex("FFFFFF");

            var layout = new StackLayout { Spacing = 0, VerticalOptions = LayoutOptions.FillAndExpand };

            Menu = new ListView
            {
                ItemsSource = MenuItem.GetItems(),
                VerticalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.Transparent
            };

            var cell = new DataTemplate(typeof(DarkTextCell));
            cell.SetBinding(TextCell.TextProperty, "Title");
            cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
            cell.SetValue(VisualElement.BackgroundColorProperty, Color.Transparent);

            Menu.ItemTemplate = cell;

            layout.Children.Add(Menu);

            Content = layout;
        }
    }
}

A MenuItem :

public class MenuItem       
{

        public static List<MenuItem> GetItems()
        {

            List<MenuItem> listItem = new List<MenuItem>();

            listItem.Add(new MenuItem()
            {
                Title = Ressources.AppResources.Home,
                Icon = "home.png",
                TargetType = typeof(ProductsPage)
            });

            listItem.Add(new MenuItem()
            {
                Title = Ressources.AppResources.MyOrders,
                Icon = "myorders.png",
                TargetType = typeof(CommandPage)
            });

            listItem.Add(new MenuItem()
            {
                Title = Ressources.AppResources.Parameters,
                Icon = "parameters.png",
                TargetType = typeof(OptionsPage)
            });

            listItem.Add(new MenuItem()
            {
                Title = Ressources.AppResources.Help,
                Icon = "help.png",
                TargetType = typeof(HelpPage)
            });

            listItem.Add(new MenuItem()
            {
                Title = Ressources.AppResources.ContactUs,
                Icon = "contactus.png",
                TargetType = typeof(ContactUsPage)
            });

            return listItem;
        }


        public string Title { get; set; }

        public string Icon { get; set; }

        public bool Selected { get; set; }

        public Type TargetType { get; set; }

        public ImageSource IconSource { get { return ImageSource.FromFile(Icon); } }

    }

And a renderer for Android and one for IOS :

public class DarkTextCell : ImageCell {}

[assembly: ExportCell(typeof(DarkTextCell), typeof(DarkTextCellRenderer))] namespace ColorPicker.Droid.Renderers { public class DarkTextCellRenderer : ImageCellRenderer { protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context) { var cell = (LinearLayout)base.GetCellCore(item, convertView, parent, context); cell.SetPadding(20, 30, 0, 30); cell.DividerPadding = 50; var div = new ShapeDrawable(); div.SetIntrinsicHeight(1); div.Paint.Set(new Paint { Color = Color.FromHex("00FFFFFF").ToAndroid() }); if (parent is ListView) { ((ListView)parent).Divider = div; ((ListView)parent).DividerHeight = 1; } var image = (ImageView)cell.GetChildAt(0); image.SetScaleType(ImageView.ScaleType.FitCenter); image.LayoutParameters.Width = 60; image.LayoutParameters.Height = 60; var linear = (LinearLayout)cell.GetChildAt(1); linear.SetGravity(Android.Views.GravityFlags.CenterVertical); var label = (TextView)linear.GetChildAt(0); label.SetTextColor(Color.Gray.ToAndroid()); label.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel(); label.Gravity = (Android.Views.GravityFlags.CenterVertical); var secondaryLabel = (TextView)linear.GetChildAt(1); secondaryLabel.Visibility = Android.Views.ViewStates.Gone; return cell; } } }

I have tried to add a click handler in the Android renderer like this :


cell.Click += (sender, args) => { label.SetTextColor(Color.Lime.ToAndroid()); };

But when I do this , the navigation didn't work anymore , like the others handlers are gone.

Thanks for your help ! :)

I'm a little bit lost...


Viewing all articles
Browse latest Browse all 75885

Trending Articles