Xamarin.Forms中ListView中选中项的背景色是什么?

4
在Xamarin.forms中使用ListView时,当选择一个项目时,该项目会保持原样,背景颜色相当丑陋。我能否禁用此功能?我想要去掉的蓝色。 这是我的代码:
<StackLayout>
  <ListView x:Name="FoodList" HasUnevenRows="True" CachingStrategy="RecycleElement" ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="0,15,0,0" >
           <StackLayout Orientation="Horizontal" BackgroundColor="White" >
            <Image Source="{Binding image_url}" Aspect="AspectFill" HeightRequest="200" WidthRequest="200"/>
            <StackLayout Orientation="Vertical">
              <Label Text="{Binding food_name}" TextColor="Black" Style="{StaticResource MainLisTtext}" />
              <Label Text="{Binding price}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
              <Label Text="{Binding food_description}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
            </StackLayout>
            </StackLayout>
          </StackLayout>
        </ViewCell>vc
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>
1个回答

17

使用自定义渲染器可以实现此目的。

iOS 自定义渲染器

在 iOS 上编辑 SelectionStyle 属性。

以下是将 UITableViewCellSelectionStyle 设置为 None 的示例。

using System;

using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using ListViewSample.iOS;

[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.iOS
{
    public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);

            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            return cell;
        }
    }
}

Android自定义渲染器

  1. 创建一个新的drawable,命名为ViewCellBackground.xml,并保存到Resources>drawable文件夹中:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <!--Change the selected color by modifying this hex value-->
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</selector>
  • 为ViewCell创建自定义渲染器

  • using System;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    using ListViewSample.Droid;
    
    [assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
    namespace ListViewSample.Droid
    {
        public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
        {
            protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
            {
                var cell = base.GetCellCore(item, convertView, parent, context);
    
                cell.SetBackgroundResource(Resource.Drawable.ViewCellBackground);
    
                return cell;
            }
        }
    }
    

    编辑:删除不包含自定义渲染器的实现


    示例 Xamarin.Forms ListView 应用程序

    using System;
    using System.Collections.Generic;
    
    using Xamarin.Forms;
    
    namespace ListViewSample
    {
        public class CustomViewCell : ViewCell
        {
            public CustomViewCell()
            {
                View = new Label
                {
                    Text = "Hello World"
                };
            }
        }
    
        public class ListViewContentPage : ContentPage
        {
            public ListViewContentPage()
            {
                var itemSourceList = new List<CustomViewCell>();
                itemSourceList.Add(new CustomViewCell());
                itemSourceList.Add(new CustomViewCell());
    
                var listView = new ListView();
                listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
                listView.ItemsSource = itemSourceList;
                listView.SeparatorVisibility = SeparatorVisibility.None;
    
                Content = listView;
            }
        }
    
        public class App : Application
        {
            public App()
            {
                // The root page of your application
    
                MainPage = new NavigationPage(new ListViewContentPage());
            }
        }
    }
    

    OnListViewTextCellTapped 这是一个自定义方法吗? - John
    检查一下我的代码,我使用XAML定义了我的ListView模板。 - John
    嘿,约翰!一切都进行得不错吗? - Brandon Minnick
    @BrandonMinnick 我在 Android 上尝试了一下。当我点击该项时,它显示了我所需的背景颜色,但是当我松开触摸时,它会逐渐消失。有什么想法吗? - Baqer Naqvi
    嘿@BaqerNaqvi!抱歉-我刚看到你的评论。你还在尝试解决这个问题吗?开一个新问题并把链接发给我吧! - Brandon Minnick
    显示剩余9条评论

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,