/
ViewProductsActivity.cs
140 lines (117 loc) · 4.03 KB
/
ViewProductsActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using EasyLayout.Droid.Sample.Models;
namespace EasyLayout.Droid.Sample
{
[Activity]
public class ViewProductsActivity : Activity
{
private RelativeLayout _relativeLayout;
private ListView _listView;
private ProductAdapter _adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetPageProperties();
AddViews();
ConstrainLayout();
SetContentView(_relativeLayout);
}
private void SetPageProperties()
{
Title = "Products ListView";
}
private void AddViews()
{
_relativeLayout = ViewUtils.AddRelativeLayout(this);
_listView = _relativeLayout.Add<ListView>();
List<Product> items = Data.GetProducts();
_adapter = new ProductAdapter(this, items);
_listView.Adapter = _adapter;
}
private void ConstrainLayout()
{
_relativeLayout.ConstrainLayout(() =>
_listView.Top == _relativeLayout.Top &&
_listView.Right == _relativeLayout.Right &&
_listView.Left == _relativeLayout.Left &&
_listView.Bottom == _relativeLayout.Bottom
);
}
}
internal class ProductAdapter : BaseAdapter<Product>
{
private readonly Context _context;
private readonly List<Product> _items;
public ProductAdapter(Context context, List<Product> items)
{
_context = context;
_items = items;
}
public override long GetItemId(int position) => _items[position].Id;
public override int Count => _items.Count;
public override Product this[int position] => _items[position];
public override View GetView(int position, View view, ViewGroup parent)
{
var productRowView = view as ProductRowView ??
new ProductRowView(_context);
var product = _items[position];
productRowView.Update(product);
return productRowView;
}
}
public class ProductRowView : RelativeLayout
{
private TextView _titleText;
private TextView _dollarText;
private TextView _amountText;
public ProductRowView(Context context) : base(context)
{
SetViewProperties();
AddViews();
ConstrainLayout(this);
}
private void SetViewProperties()
{
var height = ViewUtils.DpToPx(Context, 40);
var width = ViewGroup.LayoutParams.MatchParent;
LayoutParameters = new ViewGroup.LayoutParams(width, height);
}
private void AddViews()
{
_titleText = this.Add<TextView>();
_titleText.SetTextColor(Colors.YellowGreen);
_dollarText = AddDollarText(this);
_amountText = this.Add<TextView>();
_amountText.SetTextColor(Colors.Green);
}
private static TextView AddDollarText(ViewGroup parent)
{
var dollarText = parent.Add<TextView>();
dollarText.Text = "$";
dollarText.SetTextColor(Colors.Green);
dollarText.TextSize = 8;
return dollarText;
}
private void ConstrainLayout(RelativeLayout relativeLayout)
{
relativeLayout.ConstrainLayout(() =>
_titleText.Left == Left + 20
&& _titleText.Top == Top + 10
&& _amountText.Right == Right - 20
&& _amountText.Top == Top + 10
&& _dollarText.Right == _amountText.Left - 2
&& _dollarText.Top == _amountText.Top
);
}
public void Update(Product product)
{
_titleText.Text = product.Title;
_amountText.Text = product.Amount.ToString("0.00");
}
}
}