The background color of the selected item in the List view has been changed, but the underline of the bottom part remains.

영훈 정 281 Reputation points
2022-10-12T08:14:55.537+00:00

249605-image.png

<ListView  
                        x:Name="xListview"  
                        Grid.Row="0"  
                        ItemSelected="OnItemSelected"  
                        RowHeight="50">  
                        <ListView.ItemTemplate>  
                            <DataTemplate>  
                                <ViewCell Tapped="OnTappedListItem">  
  
                                    <Grid>  
                                        <Grid.ColumnDefinitions>  
                                            <ColumnDefinition Width="7*" />  
                                            <ColumnDefinition Width="3*" />  
                                        </Grid.ColumnDefinitions>  
  
                                        <Label  
                                            Grid.Column="0"  
                                            Margin="20,0,0,0"  
                                            Style="{StaticResource LabelStyle}"  
                                            Text="{Binding LabelText}" />  
  
                                        <StackLayout  
                                            Grid.Column="1"  
                                            Margin="0,0,5,0"  
                                            HorizontalOptions="End"  
                                            Orientation="Horizontal"  
                                            Spacing="10"  
                                            VerticalOptions="Center">  
  
                                            <Ctrl:CtrlImageButton  
                                                BackgroundColor="Transparent"  
                                                Clicked="OnModifyClicked"  
                                                HeightRequest="25"  
                                                Source="edit_button.png"  
                                                WidthRequest="25" />  
  
                                            <Ctrl:CtrlImageButton  
                                                BackgroundColor="Transparent"  
                                                Clicked="OnDeleteClicked"  
                                                HeightRequest="27"  
                                                Source="btn_delete.png"  
                                                WidthRequest="27" />  
  
                                        </StackLayout>  
                                    </Grid>  
  
                                </ViewCell>  
                            </DataTemplate>  
                        </ListView.ItemTemplate>  
                    </ListView>  


 

private async void OnTappedListItem(object sender, EventArgs e)  
        {  
            Application.Current.Resources.TryGetValue("ColorBack007", out var ColorBack007);  
  
            if (m_LastSelectViewCell != null)  
                m_LastSelectViewCell.View.BackgroundColor = Color.Transparent;  
  
            var viewCell = sender as ViewCell;  
            viewCell.View.BackgroundColor = (Color)ColorBack007;  
  
            m_LastSelectViewCell = viewCell;  
  
  
            ////viewCell.backg  
  
  
            //this.xListview.BackgroundColor = Color.Gray;   
            //await Task.Delay(10);  
            //this.xListview.BackgroundColor = Color.White;  
        }  

I changed the background color of the selected cell by using the tap event. I don't know why, but about 2 pixels of underline remain. Is there a way to completely erase it?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-10-13T07:15:18.533+00:00

    Hello,

    Yes, I can reproduce your issue. If you change the selected item color with ViewCell Tapped event, it will cause reused issue of items
    If you want to change selected item to other color. You can use the following way to do it.

    For android, you can add these lines to Resources\values\styles.xml inside the <style> tag.

       <style name="MainTheme" parent="MainTheme.Base">  
            <item name="android:colorPressedHighlight">@color/ListViewSelected</item>  
             <item name="android:colorFocusedHighlight">@color/ListViewSelected</item>  
             <item name="android:colorActivatedHighlight">@color/ListViewSelected</item>  
             <item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item>  
         </style>  
    

    Then open your Resources\values\colors.xml, add you want to changed color in it like following code.

       <resources>  
           <color name="launcher_background">#FFFFFF</color>  
           <color name="colorPrimary">#3F51B5</color>  
           <color name="colorPrimaryDark">#303F9F</color>  
           <color name="colorAccent">#FF4081</color>  
         
          <!--set your selected item background color-->  
          <color name="ListViewSelected">#96BCE3</color>  
       </resources>  
    

    For iOS, you need to create a custom renderer for ViewCell.

       [assembly: ExportRenderer(typeof(ViewCell), typeof(MyViewCellRenderer))]  
       namespace App2.iOS  
       {  
           internal class MyViewCellRenderer: ViewCellRenderer  
           {  
              public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)  
               {  
                   var cell = base.GetCell(item, reusableCell, tv);  
                  cell.SelectedBackgroundView = new UIView  
                   {  
                       //set your selected item background color.  
                       BackgroundColor = UIColor.DarkGray,  
                   };  
                  return cell;  
               }  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.