question

KellyKranabetter-5085 avatar image
0 Votes"
KellyKranabetter-5085 asked JessieZhang-2116 edited

Odd Xamarin Forms Listview contextaction Android behavior

I have created a simple ListView with a context menu:

     <ListView x:Name="listview" SelectionMode="Single">
         <ListView.ItemTemplate>
             <DataTemplate>
                 <ViewCell>
                     <StackLayout Orientation="Horizontal" >
                         <Label Text="{Binding item1}"></Label>
                         <Label Text="{Binding item2}"></Label>
                         <Label Text="{Binding item3}"></Label>
                     </StackLayout>
                     <ViewCell.ContextActions>
                         <MenuItem Text="Add" ></MenuItem>
                         <MenuItem Text="Delete"></MenuItem>
                         <MenuItem Text="Edit"></MenuItem>
                     </ViewCell.ContextActions>
                 </ViewCell>
             </DataTemplate>
         </ListView.ItemTemplate>
     </ListView>

Development environment is Visual Studio 16.9.1, Xamarin Forms Nuget package is at 5.0.0.2012 which I believe are the latest. Pictures below are of the Android Pixel2 Emulator.

Selecting an item works ok:
77348-image.png

A long press on the selected rows activates the menu but disables the selection background. Which row is the operation on?:
77354-image.png

If I tap around randomly on other rows they may select or may deselect:
77289-image.png

When I click the back button to exit the context menu the selections change again (notice the selections are different than the above image):
77349-image.png

If I tap on each item they will deselect and return to a single selection.

I have SelectionMode="Single" and still seem to get multiple selections. What am I doing wrong?



dotnet-xamarin
image.png (16.4 KiB)
image.png (16.4 KiB)
image.png (16.9 KiB)
image.png (16.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 edited

Hello,


Welcome to our Microsoft Q&A platform!

In android, you can simply add following tag(s) to styles.xml (location: Android project > Resources > values > styles.xml)

    <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>

And add color in file colors.xml:

 <color name="ListViewHighlighted">#FFFF00</color>

The result is:

77497-image.png



Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




image.png (134.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.