ListView not unselecting

后端 未结 4 947
北海茫月
北海茫月 2021-01-14 23:19

I am working on a Xamarin.Forms project with a ListView.

The XAML for the ListView is



        
4条回答
  •  执笔经年
    2021-01-14 23:49

    To apply background color to list view selected item follow these steps:

    Make custom control:

    using Xamarin.Forms;
    
    namespace xamformsdemo.CustomControls
    {
      public class ExtendedViewCell : ViewCell
      {
        public static readonly BindableProperty SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor", 
                                typeof(Color), 
                                typeof(ExtendedViewCell), 
                                Color.Default);
    
        public Color SelectedBackgroundColor
        {
           get { return (Color)GetValue(SelectedBackgroundColorProperty); }
           set { SetValue(SelectedBackgroundColorProperty, value); }
        }
      }
    }
    

    Android Renderer:

    [assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
    namespace xamformsdemo.Droid.CustomRenderers
    {
      public class ExtendedViewCellRenderer : ViewCellRenderer
      {
    
    private Android.Views.View _cellCore;
    private Drawable _unselectedBackground;
    private bool _selected;
    
    protected override Android.Views.View GetCellCore(Cell item, 
                                                      Android.Views.View convertView, 
                                                      ViewGroup parent, 
                                                      Context context)
    {
      _cellCore = base.GetCellCore(item, convertView, parent, context);
    
      _selected = false;
      _unselectedBackground = _cellCore.Background;
    
      return _cellCore;
    }
    
    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
      base.OnCellPropertyChanged(sender, args);
    
      if (args.PropertyName == "IsSelected")
      {
        _selected = !_selected;
    
        if (_selected)
        {
          var extendedViewCell = sender as ExtendedViewCell;
          _cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
        }
        else
        {
          _cellCore.SetBackground(_unselectedBackground);
        }
      }
    }
      }
    
    }
    

    iOS Renderer:

    [assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
    namespace xamformsdemo.iOS.CustomRenderers
    {
      public class ExtendedViewCellRenderer : ViewCellRenderer
      {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
       {
          var cell = base.GetCell(item, reusableCell, tv);
          var view = item as ExtendedViewCell;
          cell.SelectedBackgroundView = new UIView
           {
             BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
           };
    
          return cell;
        }
    
      }
    }
    

    Use in XAML:

    
      
        
          
            
              
          
        
      
    
    

    You can refer this link below to solve problem: https://blog.wislon.io/posts/2017/04/11/xamforms-listview-selected-colour

提交回复
热议问题