【问题标题】:How to disable selection highlight in listview xamarin.forms on android如何在android上的listview xamarin.forms中禁用选择突出显示
【发布时间】:2014-11-06 16:18:10
【问题描述】:

我使用 xamarin.forms 创建了列表视图,我正在寻找一种在点击列表视图时不突出显示视图单元的方法。

请查看下图。

提前致谢:)

【问题讨论】:

    标签: android listview xamarin.forms


    【解决方案1】:

    在您的 ListView SelectedItem 事件处理程序上,您可以这样做:

    listview.SelectedItem = null;
    

    这将为您提供点击突出显示,但状态将只是暂时的。

    在你的情况下,我想你会喜欢这个,因为你使用 2 Images 而不是 Buttons 作为右边的箭头,TapGestureRecognizer。你知道Button 有一个Image 属性吗?在Cell 中单击Button 时,Cell 的选定状态不应更改。

    【讨论】:

    • 当我点击图片时,它不会改变列表视图的状态。仅当我错误地单击图像以外的列表时,它才会更改,并且看起来很奇怪。我尝试了您的解决方案,但它不起作用..更改了视图单元格的背景颜色,它成功了.. :) 感谢您的帮助..:) 我也不知道按钮具有图像属性,这也感谢您。 .
    • ItemSelected 不可为空
    • 你的意思是 .SelectedItem ItemSelected 是事件。 SelectedItem 是属性。
    • listview.SelectedItem = null;
    【解决方案2】:

    只需将其放入您的自定义主题中即可:

    <item name="android:colorActivatedHighlight">@android:color/transparent</item>
    

    【讨论】:

    • 谢谢。有用。在 droid 项目中添加这一行的默认文件位于 Resouces/values/styles.xml
    • 这工作得很好...我也想拥有 android 项目的实现...
    【解决方案3】:

    你不能,你必须实现一个自定义渲染。如果您只是将选定项目设置为空,它将删除选定的颜色。但是您将首先选择该项目,然后再次取消选择它(多个事件)但您没有看到它:-)。如果您在 Windows Phone 上启用了倾斜效果,由于所有事件,倾斜仍然会发生!

    但我希望看到 Xamarin Forms 团队在列表视图上实现 CanSelect 属性。

    【讨论】:

      【解决方案4】:

      我想建议您另一种解决方案。 您可以添加:

      IsEnabled="False"
      

      在您的列表视图小部件中。 就我而言,这个解决方案效果很好。

      【讨论】:

      • 谢谢。非常适合我!
      • @TeoVr81 这将禁用列表中的每个事件,而不仅仅是选择,例如你将无法滚动
      【解决方案5】:

      对于在 Android 和 iOS 中禁用 Listview 单元格选择突出显示,此链接非常有用

      https://gist.github.com/jessejiang0214/63b29b3166330c6fc083

      ProjectName.Android/Resources/values/styles.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <resources>
          <style name="CustomTheme" parent="android:Theme.Holo.Light">
              <item name="android:colorActivatedHighlight">@android:color/transparent</item>
          </style>
      </resources>
      

      CustomAllViewCellRendereriOS.cs

      using System;
      using Xamarin.Forms;
      using Xamarin.Forms.Platform.iOS;
      
      [assembly: ExportRenderer (typeof(ViewCell), typeof(MyAPP.iOS.CustomAllViewCellRendereriOS))]
      namespace MyAPP.iOS
      {
          public class CustomAllViewCellRendereriOS : ViewCellRenderer
          {
              public override UIKit.UITableViewCell GetCell (Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
              {
                  var cell =  base.GetCell (item, reusableCell, tv);
                  if (cell != null)
                      cell.SelectionStyle = UIKit.UITableViewCellSelectionStyle.None;
                  return cell;
              }
          }
      }
      

      这似乎是正确和最好的方式......!

      【讨论】:

        【解决方案6】:

        我想分享另一个我非常喜欢的解决方案,因为它很简单,你必须实现一次,而且在 XAML 中非常容易使用。

        首先我们必须实现我们自己的行为。这很简单:

        public class DeselectItemBehavior : Behavior<ListView>
        {
            protected override void OnAttachedTo(ListView bindable)
            {
                base.OnAttachedTo(bindable);
        
                bindable.ItemSelected += ListView_ItemSelected;
            }
        
            protected override void OnDetachingFrom(ListView bindable)
            {
                base.OnDetachingFrom(bindable);
        
                bindable.ItemSelected -= ListView_ItemSelected;
            }
        
            private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
            {
                ((ListView)sender).SelectedItem = null;
            }
        }
        
        

        所以我们只是在设置行为时注册事件,并在未设置行为时取消注册。

        活动本身使用 Stephane Delcroix 方法。

        现在你需要在 ListView 中添加如下行为:

        <ListView ...>
            <ListView.Behaviors>
                <behaviors:DeselectItemBehavior />
            </ListView.Behaviors>
        

        【讨论】:

          【解决方案7】:

          我在 Xamarin.Forms 中使用:ListView SelectionMode = "None"

          【讨论】:

          • THIS 应该是公认的答案(这是最简单和最新的解决方案)
          • 这会停用 SelectionChanged 功能
          猜你喜欢
          • 2014-12-26
          • 1970-01-01
          • 2016-07-31
          • 2011-02-23
          • 2017-07-15
          • 2014-09-18
          • 1970-01-01
          • 2013-06-06
          • 2011-03-10
          相关资源
          最近更新 更多