【问题标题】:ListView ContextMenu Active State ColorListView ContextMenu 活动状态颜色
【发布时间】:2017-04-19 22:27:55
【问题描述】:

我正在尝试让我的 ListView 具有三种背景颜色状态。

  1. 正常
  2. 按下(当用户的手指触摸时)
  3. 上下文菜单处于活动状态(当上下文菜单针对特定项目出现时)

注意:当上下文菜单未激活时,我不需要选择状态。

基本上,我遇到困难的部分是#3。当用户长按列表视图项目时,我希望显示上下文菜单,但我也希望他们按下的项目突出显示不同的颜色。

在代码中,我有这个

// note: the names are just to tell you what view type we're dealing with.
// android_Widgit_ListView: the ListView
// android_Views_View: the Cell
if (android_Widgit_ListView != null)
{
    android_Views_View.Background = ContextCompat.GetDrawable(context, Resource.Drawable.listview_selector);
    android_Widgit_ListView.SetSelector(Resource.Drawable.listview_selector);
    android_Widgit_ListView.CacheColorHint = Color.Transparent.ToAndroid();
}

我有一个选择器(注意不同的颜色只是为了看看发生了什么)

<!-- listview_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true" android:drawable="@color/PrimaryMediumBlue"/>
  <item android:state_activated="true" android:drawable="@color/PrimaryYellow"/>
  <item android:state_selected="true" android:drawable="@color/SecondaryWarmGrey"/>
  <item android:state_checked="true" android:drawable="@color/SecondaryGreen"/>
  <item android:state_pressed="true" android:drawable="@color/SecondaryLightGrey"/>
  <item android:drawable="@color/white"/>
</selector>

问题是当我长按该项目以调出上下文菜单时,列表视图项目返回白色。

如何使该状态保持不同的颜色?

【问题讨论】:

    标签: android listview xamarin


    【解决方案1】:

    这是一种解决方法,但是...

    您可以将颜色更改功能绑定到上下文菜单中的属性。

    例如:一个名为“active”的布尔值,您设置了“OnAppering”和“OnDisapering”。

    实现 INotifyPropertyChanged 并在活动属性更改时触发事件。

    然后在列表视图中监听事件并更改其颜色。

    -

    我不确定这是最好的方法...

    【讨论】:

    • 我走了那条路。 Xamarin.Forms 具有内部/密封的所有 ContextMenu 位 :(
    • 很遗憾听到这个消息。
    【解决方案2】:
    <style name="MyTheme" parent="MyTheme.Base">
      <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
    </style>
    <color name="ListViewHighlighted">#A8A8A8</color>
    

    将此添加到Resourses&gt;Values&gt;Styles.xml

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      相关资源
      最近更新 更多