Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableView section header in android is blank and clickable

I'm using Xamarin.Forms, currently trying to make a TableView without a section header. Right now on iOS this looks fine as the section header is not visible or clickable, but on Android the header is blank, visible, and clickable.

I've tried this http://forums.xamarin.com/discussion/18037/tablesection-w-out-header

Code in xaml -

<TableView>
    <TableView.Root>
      <TableSection>
        <TextCell Text="Login" Command="{Binding Login}" />
        <TextCell Text="Sign Up" Command="{Binding SignUp}" />
        <TextCell Text="About" Command="{Binding About}"/>
      </TableSection>
    </TableView.Root>
  </TableView>

Code in c#

Content = new TableView
{
    Root = new TableRoot 
    {
        new TableSection () 
        {
            new TextCell { Text="Login", Command = Login }, 
            new TextCell { Text="Sign Up", Command = SignUp },
            new TextCell { Text="About", Command = About },
        },
    },
};
like image 257
Bryan Avatar asked Jun 26 '14 21:06

Bryan


1 Answers

To suppress the header on Android, we use a custom renderer. If Text is empty, it hides the cell by removing all children, reducing the height and removing all padding.

[assembly: ExportRenderer(typeof(TextCell), typeof(ImprovedTextCellRenderer))]

namespace YourSolution.Android
{
    public class ImprovedTextCellRenderer : TextCellRenderer
    {
        protected override global::Android.Views.View GetCellCore(Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
        {
            var view = base.GetCellCore(item, convertView, parent, context) as ViewGroup;
            if (String.IsNullOrEmpty((item as TextCell).Text)) {
                view.Visibility = ViewStates.Gone;
                while (view.ChildCount > 0)
                    view.RemoveViewAt(0);
                view.SetMinimumHeight(0);
                view.SetPadding(0, 0, 0, 0);
            }
            return view;
        }
    }
}

Just copy this class somewhere into your Android project and you should be fine.

like image 108
Falko Avatar answered Nov 05 '22 09:11

Falko