forked from OfficeDev/ui-fabric-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateViewActivity.kt
134 lines (110 loc) · 5.04 KB
/
TemplateViewActivity.kt
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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.officeuifabricdemo.demos
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.microsoft.officeuifabricdemo.DemoActivity
import com.microsoft.officeuifabricdemo.R
import com.microsoft.officeuifabricdemo.demos.views.Cell
import com.microsoft.officeuifabricdemo.demos.views.CellOrientation
import kotlinx.android.synthetic.main.activity_template_view.*
import kotlinx.android.synthetic.main.template_cell_vertical.view.*
class TemplateViewActivity : DemoActivity() {
companion object {
const val LIST_ITEM_COUNT = 1000
}
override val contentLayoutId: Int
get() = R.layout.activity_template_view
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
horizontal_cell.setOnClickListener { onCellClicked(it as Cell) }
vertical_cell.setOnClickListener { onCellClicked(it as Cell) }
template_list_view.adapter = TemplateListViewAdapter()
template_list_view.layoutManager = LinearLayoutManager(this)
template_list_view.setHasFixedSize(true)
regular_list_view.adapter = RegularListViewAdapter()
regular_list_view.layoutManager = LinearLayoutManager(this)
regular_list_view.setHasFixedSize(true)
calculate_cells_button.setOnClickListener {
val t = measureAndLayoutViews(createView = {
val cell = Cell(this)
cell.orientation = CellOrientation.VERTICAL
return@measureAndLayoutViews cell
})
println("Cell.M&L: $t")
calculate_cells_button.text = getString(R.string.calculate_cells) + " = $t ms"
}
calculate_layouts_button.setOnClickListener {
val t = measureAndLayoutViews(createView = {
// Emulation of Cell code without extra ViewGroup (Cell itself)
val cell = layoutInflater.inflate(R.layout.template_cell_vertical, null)
/*val titleView = */cell.findViewById(R.id.cell_title) as TextView
/*val descriptionView = */cell.findViewById(R.id.cell_description) as TextView
return@measureAndLayoutViews cell
})
println("Layout.M&L: $t")
calculate_layouts_button.text = getString(R.string.calculate_layouts) + " = $t ms"
}
}
private fun measureAndLayoutViews(createView: () -> View): Long {
val t1 = System.nanoTime()
for (i in 1..100) {
val cell = createView()
cell.requestLayout()
cell.measure(0, 0)
cell.layout(0, 0, cell.measuredWidth, cell.measuredHeight)
}
val t2 = System.nanoTime()
return (t2 - t1) / 1000000
}
private fun onCellClicked(cell: Cell) {
cell.orientation = when (cell.orientation) {
CellOrientation.HORIZONTAL -> CellOrientation.VERTICAL
CellOrientation.VERTICAL -> CellOrientation.HORIZONTAL
}
}
// Template list view adapter
private class TemplateListViewAdapter : RecyclerView.Adapter<TemplateListViewAdapter.ViewHolder>() {
override fun getItemCount(): Int = LIST_ITEM_COUNT
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val cell = Cell(parent.context)
cell.orientation = CellOrientation.VERTICAL
cell.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
return ViewHolder(cell)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.cell.apply {
title = "Title $position"
description = "Description $position"
}
}
class ViewHolder(val cell: Cell) : RecyclerView.ViewHolder(cell)
}
// Regular list view adapter
private class RegularListViewAdapter : RecyclerView.Adapter<RegularListViewAdapter.ViewHolder>() {
override fun getItemCount(): Int = LIST_ITEM_COUNT
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val cell = LayoutInflater.from(parent.context).inflate(R.layout.template_cell_vertical, parent, false)
cell.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
return ViewHolder(cell)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.apply {
titleView.text = "Title $position"
descriptionView.text = "Description $position"
}
}
class ViewHolder(cell: View) : RecyclerView.ViewHolder(cell) {
val titleView: TextView = cell.cell_title
val descriptionView: TextView = cell.cell_description
}
}
}