2016-02-23 16 views
23

Tôi có một ListView với một ViewCell tùy chỉnh hiển thị các bài viết. Tuy nhiên khi bạn chọn một mục, nó sẽ hiển thị hiệu ứng gợn/chọn thiết kế material design.Xamarin.Form ListView không thể chỉnh sửa (loại bỏ hiệu ứng gợn lựa chọn)

Ripple effect

XAML:

<ListView HasUnevenRows="True" ItemsSource="{Binding NewsArticles}" IsPullToRefreshEnabled="True"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
      <ViewCell> 
       <StackLayout Padding="10"> 
       <Label Text="{Binding Title}" HorizontalOptions="Center" FontAttributes="Bold" /> 
       <Image Source="{Binding ImageUrl}" IsVisible="{Binding HasImage}" /> 
       <Label Text="{Binding Content}"></Label> 
       </StackLayout> 
      </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     </ListView> 

Làm thế nào để loại bỏ các hiệu ứng gợn?

Cập nhật:

Sau khi thực hiện một số nghiên cứu tôi thấy RepeaterView trong thư viện XLabs. Điều này làm việc nếu bạn quấn nó trong một ScrollView mặc dù nó lấy đi rất nhiều cơ chế ListView gọn gàng như làm mới. Nó cũng không hỗ trợ các thay đổi đối với ItemSource từ một luồng ngoài. Vẫn đang tìm kiếm các giải pháp khác vì tôi biết điều này là có thể trong Android gốc.

Trả lời

15

Vì vậy, sau một thời gian dài, chúng tôi đã tìm ra, bạn có thể thực hiện nó với trình kết xuất tùy chỉnh. Đây là cách thức,

Đầu tiên, tạo một tệp có tên no_selector.xml và đặt nó trong thư mục Tài nguyên/bố cục (thuộc tính đóng gói phải được đặt thành AndroidResource).

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_window_focused="false" android:drawable="@android:color/transparent"/> 
</selector> 

Sau đó tạo một renderer tùy chỉnh cho các thành phần ListView,

[assembly: ExportRenderer (typeof(ListView), typeof(NoRippleListViewRenderer))] 
namespace Your.Own.Namespace 
{ 
    public class NoRippleListViewRenderer : ListViewRenderer 
    { 
     protected override void OnElementChanged (ElementChangedEventArgs<ListView> e) 
     { 
      base.OnElementChanged (e); 
      Control.SetSelector (Resource.Layout.no_selector); 
     } 
    } 
} 

Nếu file no_selector không thể tìm thấy xây dựng lại dự án của bạn!

Lưu ý rằng thực tế việc này sẽ loại bỏ gợn cho tất cả các ListView trong ứng dụng của bạn. Nếu bạn chỉ muốn nó nhắm mục tiêu một cặp vợ chồng, bạn có thể thay đổi loại đầu tiên trên thuộc tính ExportRenderer (điều này đòi hỏi bạn phải tạo một lớp riêng biệt mở rộng ListView).

https://gist.github.com/awatertrevi/d83787dbbf3de6ef0e0a344169d3c2fa

+0

Chúng tôi đã cố gắng thực hiện chính xác như thế này, nhưng nhận được một NotFoundException. Chúng tôi xây dựng lại và làm sạch giải pháp nhiều lần.BuildAction được đặt thành AndroidResources và chúng ta có thể thấy tệp xml-File trong APK đã tạo – Chris

1

Tôi nghĩ bạn có thể gỡ bỏ nó mà không có một renderer tùy chỉnh.

Bạn có thể đặt BackgroundColor trong số StackPanel thành màu xám hoặc bất kỳ màu nền nào của danh sách hoặc trang của bạn.

<ListView HasUnevenRows="True" ItemsSource="{Binding NewsArticles}" IsPullToRefreshEnabled="True"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <StackLayout Padding="10" BackgroundColor="Gray"> 
        <!-- ... --> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

Hoặc bạn có thể sử dụng phong cách android để thay đổi phong cách xem danh sách:

Set chủ đề trong AndroidManifest.xml

<application android:label="$safeprojectname$" android:theme="@style/myTheme"></application> 

Tạo chủ đề trong styles.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources> 
    <style name="myTheme" parent="@android:style/Theme.Material.Light"> 
     <item name="android:listViewStyle">@style/ListViewStyle.Light</item> 
    </style> 

    <style name="ListViewStyle.Light" parent="@android:style/Widget.ListView"> 
     <item name="android:listSelector">@android:color/transparent</item> 
    </style> 
</resources> 
Các vấn đề liên quan