2016-12-04 3 views
3

При работе с ListView в Xamarin.forms при выборе элемента элемент остается таким же, как у довольно уродливого цвета фона. Могу ли я отключить эту функцию? The blue colour is what i wish to take out.Цвет фона выбранного элемента в listview xamarin.forms

Вот мой код:

<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> 

ответ

8

Это может быть достигнут с помощью пользовательского Renderers

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 Пользовательские Renderer

  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. Создание пользовательских Renderer для 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; 
         } 
        } 
    } 
    

Edit: Удалены реализация без пользовательских Renderers


Примеры Xamarin.Forms ListView App

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()); 
     } 
    } 
} 
+0

OnListViewTextCellTapped это пользовательский метод ?? –

+0

Да, прошу прощения! Я просто обновил свой ответ, включив метод OnListViewTextCellTapped. –

+0

Ok.Когда с помощью listview у меня есть доступ к выбранному элементу, применимо ли это здесь? –

Смежные вопросы