Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Customizing ListView Display in Xamarin

DZone's Guide to

Customizing ListView Display in Xamarin

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

As the usual behavior of ListView to display the items in a list and it contains a single column. The ItemSource property of Listview accepts the list of data to be displayed and render the same on screen.

The code for a simple list to display the Students of a class would like as in the following pic.

ListViewInXamarinForms

Now let’s customize this ListView in a way to display the Student Name along with the Class and School they belongs to.

ListViewItem

As in the image above, the data contains a single record. So to accommodate the data in a single record, Let’s create a Student model class.

  public class Student 
    {
        public string StudentName { get; set; }
        public string Class { get; set; }
        public string School { get; set; }
    }

In order to customize the ListView display, we need to override theViewCell class.

    public class StudentCell : ViewCell 
    {
        public StudentCell() 
        {
            Label nameCell = new Label
            {
                TextColor = Color.Blue,
                FontSize = 30
            };
            nameCell.SetBinding(Label.TextProperty, "StudentName");

            Label classCell = new Label
            {
                TextColor = Color.Gray,
                FontSize = 20
            };
            classCell.SetBinding(Label.TextProperty, "Class");

            Label schoolCell = new Label
            {
                TextColor = Color.Gray,
                FontSize = 20
            };
            schoolCell.SetBinding(Label.TextProperty, "School");

            View = new StackLayout()
            {
                Children = { nameCell, classCell, schoolCell}
            };
        }
    }

We are done with the Model and the inherited ViewCell class i.e.StudentCell. We will use the ItemTemplate attribute of ListView to assign the StudentCell class.

 List<Student> students = new List<Student>();
            students.Add(new Student() { StudentName = "Nirmal Hota", Class = "10th", School = "Bhagabati High School" });
            students.Add(new Student() { StudentName = "Tadit Dash", Class = "6th", School = "Student High School" });
            students.Add(new Student() { StudentName = "Suraj Sahoo", Class = "5th", School = "Khannagar High School" });
            students.Add(new Student() { StudentName = "Suvendu Giri", Class = "9th", School = "Baleswar High School" });
            students.Add(new Student() { StudentName = "Subhasish Pattanaik", Class = "8th", School = "Rourkela High School" });
            // The root page of your application
            MainPage = new ContentPage
            {
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
						new ListView(){
                            ItemsSource = students,
                            ItemTemplate = new DataTemplate(typeof(StudentCell)),
                            HorizontalOptions=LayoutOptions.FillAndExpand
                        }
					}
                }
            };

Let’s run the app to see the new ListView.

Happy coding :)


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Nirmal Hota, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}