2017-04-19 52 views
0

我想让我的ListView有三种背景颜色状态。ListView ContextMenu活动状态颜色

  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> 

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

如何获得该状态以保持不同的颜色?

回答

0

它有点变通方法,但...

你可以绑定变化颜色的功能,在上下文菜单的属性。

例如:您设置了“OnAppering”和“OnDisapering”的称为“活动”的bool。

执行INotifyPropertyChanged并在活动属性更改时触发事件。

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

-

我不知道这是最好的办法......

+0

我就沿着这条路走下去添加此。 Xamarin.Forms内部/密封所有的ContextMenu位:( –

+0

达姆抱歉听到这一点。 – rrr

0
<style name="MyTheme" parent="MyTheme.Base"> 
    <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item> 
</style> 
<color name="ListViewHighlighted">#A8A8A8</color> 

Resourses>Values>Styles.xml