列表视图xamarin.forms中所选项目的背景颜色

问题描述 投票:0回答:1

在 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>
c# .net xamarin xamarin.forms xamarin.forms.listview
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. 创建一个新的绘图,

    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>
    
  2. 为 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());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.