Reputation: 4001
The ListView
in Xamarin.Forms has a RowHeight
property, but it seems no matter what height I set it to.
I use Visual Studio 2013, I have the business edition license for Xamarin and I use the Android emulator MonoForAndroid_API_15 and i believe I have the latest version of everything involved. As of yet I can't run iOS or WinPhone emulators, so I can't compare. Is this just an issue of the Android emulator or is setting the RowHeight
property not correct?
Here is my inherited ContentPage
(note that I set RowHeight
):
public class QuizzesPage : ContentPage
{
private readonly ListView _listView;
public QuizzesPage()
{
Title = "Test";
NavigationPage.SetHasNavigationBar(this, true);
_listView = new ListView {RowHeight = 20};
Content = _listView;
}
protected async override void OnAppearing()
{
base.OnAppearing();
_listView.ItemsSource = new[] {"One", "Two", "Three", "Four", "Five"};
}
}
In App
:
public class App
{
public static Page GetMainPage()
{
return new NavigationPage(new QuizzesPage());
}
}
And this is the visual result:
Note that my code is a simplified version of the TODO example used by Xamarin, where you see them set the RowHeight
property in the same way as I do.
No matter what I set as RowHeight
the rows have the same huge height. What can I do?
Edit 1: I plan to upgrade to Windows 8.1 this week and will then be able to test how it looks on WinPhone. To add a Mac to the network at work I need some permissions first. This is why I am stuck with only the Android emulator as of yet.
Edit 2: I tried setting HasUnevenRows
to true, as suggested here, but that only made it accept longer entries, not change the height (or font) of the row, which is way too large for my taste.
Edit 3: I posted a solution/workaround as an answer. At least I get full control over the height of the cells. But RowHeight
does not seem to do much.
Upvotes: 3
Views: 5050
Reputation: 4001
I got this suggestion:
Not sure about RowHeight but try setting ListView.HasUnevenRows to true. Then you can specify the height for your cells. Could be that setting this bool is also required when using RowHeight.
This made me realise that I had to create a class that inherits from ViewCell
and bind to a property on a class:
public class QuizCell : ViewCell
{
public QuizCell()
{
var label = new Label {HeightRequest = 20};
label.SetBinding (Label.TextProperty, "Title");
View = label;
}
}
To use this I changed QuizzesPage
to this:
public class QuizzesPage : ContentPage
{
private readonly ListView _listView;
public QuizzesPage()
{
Title = "Test";
NavigationPage.SetHasNavigationBar(this, true);
_listView = new ListView
{
HasUnevenRows = true,
ItemTemplate = new DataTemplate(typeof (QuizCell)),
//RowHeight = 10
};
Content = _listView;
}
protected async override void OnAppearing()
{
base.OnAppearing();
var quizEndpoint = new QuizEndpoint("Test");
var quizzes = await quizEndpoint.LoadAllAsync();
_listView.ItemsSource = quizzes;
}
}
The important parts here are:
ListView
s ItemSource
is set to a List<Quiz>
where the Quiz
object has a Title
property (the one I bound the Label
to).ListView
sets it ItemTemplate
method to new DataTemplate(typeof (QuizCell))
, which is the new class I created.Now I can set the height to a row to whatever I want using the HeightRequest
property of the component used in class inherited from ViewCell
.
The reason for my problems was that I simplified the TODO example too much (and that RowHeight
does not seem to accomplish much). Also, in conclusion, this had nothing to do with the emulator. I hope this helps someone!
Upvotes: 6