Background
Suppose there is a business requirement such as List of elements (List View) and inside each element there is another list of elements (List View) which looks like a horizontal list, then the Horizontal list view inside list view concept of Xamarin Android comes into the picture.
I got such a requirement hence I got a solution which I am explaining as follows:
How to develop this use case. Let's understand it step by step,
- First step is to create blank android app project and give some meaningful name to it.
- After creation of project there is a class file called MainActivity.cs which is the default start activity of your project same like Public Static Main() method in Console application as well as Main.axml file also present which is default Activity Layout which is set to MainActivity.cs
- Add ListView to Main.axml and assign some unique Id to ListView.
- Set Main.axml page to OnCreate() method of MainActivity.cs
- Created New ViewModel folder.
- Created new class file and named as EmployeeViewModel.
- Code for EmployeeViewModel is as follows,
- using System.Collections.Generic;
- using HorizontalListViewInsideListViewDemo.Model;
- namespace HorizontalListViewInsideListViewDemo.ViewModel {
- public class EmployeeViewModel {
- Employee emp1, emp2, emp3, emp4, emp5, emp6;
- private List < Employee > _employeeList;
- public List < Employee > EmployeeList {
- get {
- return _employeeList;
- }
- set {
- _employeeList = value;
-
- }
- }
- public EmployeeViewModel() {
- emp1 = new Employee() {
- Name = "Amit",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- emp2 = new Employee() {
- Name = "Virendra",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- emp3 = new Employee() {
- Name = "Mehul",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- emp4 = new Employee() {
- Name = "LK",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- emp5 = new Employee() {
- Name = "Shradhha",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- emp6 = new Employee() {
- Name = "Nilam",
- TaskList = new List < string > {
- "Task 1",
- "Task 2",
- "Task 3"
- }
- };
- }
- public void GetEmployeeList() {
- EmployeeList = new List < Employee > ();
- EmployeeList.Add(emp1);
- EmployeeList.Add(emp2);
- EmployeeList.Add(emp3);
- EmployeeList.Add(emp4);
- EmployeeList.Add(emp5);
- EmployeeList.Add(emp6);
- }
- }
- }
- Add new folder as Model.
- Add new class file named as Employee.cs in Model folder.
- Code for Employee.cs is as follows,
- Added new Android Layout in Resources->Layout folder as “employeeListViewItems”
- Designing code for” employeeListViewItems.axml” is as follows,
- Code for employeeListViewItems.axml is as follows,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/white">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/empName"
- android:layout_margin="10dp"
- android:textColor="@android:color/darker_gray"
- android:textSize="20dp"
- android:textStyle="bold" />
- <HorizontalScrollView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:scrollbarAlwaysDrawHorizontalTrack="false"
- android:scrollbars="none"
- android:layout_marginLeft="5dp"
- android:layout_marginTop="15dp"
- android:layout_marginBottom="20dp"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:descendantFocusability="blocksDescendants">
- <LinearLayout
- android:id="@+id/mylist"
- android:background="#ace6df"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:descendantFocusability="blocksDescendants" />
- </HorizontalScrollView>
- </LinearLayout>
- Added Nuget package for MVVM,
- Now we have to added horizontal list view programmatically, we can add this List View in OnResume() method of android Lifecycle.
- First we create list of Employee in EmployeeViewModel and store the list of Employee in EmployeeList which is a List<Employee>;
- Then Create a Horizontal List View as per Task assigned to respected Employee.
- Code of MainActivity.cs is as follows,
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using HorizontalListViewInsideListViewDemo.ViewModel;
- using System;
- using GalaSoft.MvvmLight.Helpers;
- using Android.Views;
- using HorizontalListViewInsideListViewDemo.Model;
- using Android.Graphics;
- using Android.Util;
-
- namespace HorizontalListViewInsideListViewDemo
- {
- [Activity(Label = "Horizontal List View Page", MainLauncher = true, Icon= "@drawable/icon")]
- public class MainActivity : Activity
- {
- ListView employeeList;
- EmployeeViewModel employeeViewModel;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView(Resource.Layout.Main);
- employeeViewModel = new EmployeeViewModel();
- employeeList = FindViewById<ListView>(Resource.Id.listEmployee);
- }
-
- protected override void OnResume()
- {
- base.OnResume();
- try
- {
- employeeViewModel.GetEmployeeList();
-
-
- if (employeeViewModel.EmployeeList != null)
- {
- RefreshList();
- }
-
- }
- catch (Exception)
- {
- }
- finally
- {
-
- }
-
- }
-
- private void RefreshList()
- {
- employeeList.Adapter = employeeViewModel.EmployeeList.GetAdapter(GetListAdapter);
- }
-
- private View GetListAdapter(int arg1, Employee emp, View view)
- {
- var empListView = LayoutInflater.Inflate(Resource.Layout.employeeListViewItems, null);
-
- if (empListView != null)
- {
- empListView.FindViewById<TextView>(Resource.Id.empName).Text = emp.Name;
-
-
-
- #region Horizontal ScrollView
- var empTaskList = emp.TaskList;
- var count = emp.TaskList.Count;
- foreach (var taskName in empTaskList)
- {
- var button = new Button(this)
- {
- Text = taskName,
- };
-
- button.SetTextColor(Color.DarkGray);
- button.SetTextSize(ComplexUnitType.Px, 40);
- button.SetPadding(5, 3, 5, 3);
- button.SetMaxHeight(80);
- button.SetMaxWidth(150);
- button.SetMinWidth(150);
- button.SetMinHeight(80);
- button.SetMinimumHeight(80);
- button.SetMinimumWidth(150);
- button.SetBackgroundColor(Color.White);
-
- ((LinearLayout)empListView.FindViewById(Resource.Id.mylist)).AddView(button);
-
- }
- #endregion
-
- }
- return empListView;
- }
-
- protected override void OnPause()
- {
- base.OnPause();
- }
- protected override void OnDestroy()
- {
- base.OnDestroy();
- }
- public override void OnBackPressed()
- {
-
- }
- }
- }
Output window for Horizontal List View Inside List View in Xamarin Android,
Note
If any doubt regarding this topic please comment in Comments box below, I will get back to you as early as possible.
Summary
Hence we will learn how to create Horizontal List View inside List View in Xamarin Android.