瀏覽代碼

Add automatic button organization

pull/1/head
Malte Schmitz 8 年之前
父節點
當前提交
5c8a8e2153
共有 2 個文件被更改,包括 25 次插入16 次删除
  1. +16
    -7
      app/src/main/java/de/mlte/soundboard/MainActivity.kt
  2. +9
    -9
      app/src/main/java/de/mlte/soundboard/SoundButton.kt

+ 16
- 7
app/src/main/java/de/mlte/soundboard/MainActivity.kt 查看文件

@@ -45,8 +45,9 @@ class MainActivity : AppCompatActivity() {
if (item != null) {
when (item.itemId) {
R.id.action_add_new -> {
val button = SoundButton(this, 0,2)
val button = SoundButton(this)
addButton(button)
organizeButtons()
return true
}
}
@@ -55,13 +56,21 @@ class MainActivity : AppCompatActivity() {
}

private fun spawnButtons() {
for (i in 0..4) {
val soundButton = SoundButton(this)
addButton(soundButton)
}
organizeButtons()
}

private fun organizeButtons() {
val parent = findViewById<GridLayout>(R.id.grid_layout)
parent.columnCount = 2
for (row in 0..1) {
for (col in 0..1) {
val soundButton = SoundButton(this, col, row)
addButton(soundButton)
}
val columns = Math.min(Math.ceil(buttons.size / 4.0).toInt(), 4)
parent.columnCount = columns
buttons.forEachIndexed { index, soundButton ->
val col = index % columns
val row = index / columns
soundButton.move(col, row)
}
}



+ 9
- 9
app/src/main/java/de/mlte/soundboard/SoundButton.kt 查看文件

@@ -14,16 +14,9 @@ class SoundButton : FrameLayout {
val btn: TextView
val objectAnimator: ObjectAnimator

constructor(context: Context, col: Int, row: Int) : super(context) {
constructor(context: Context) : super(context) {
View.inflate(context, R.layout.layout_button, this)

val params = GridLayout.LayoutParams()
params.columnSpec = GridLayout.spec(col, 1, 1.0f)
params.rowSpec = GridLayout.spec(row, 1, 1.0f)
params.width = 0
params.height = 0
layoutParams = params

progressBar = findViewById<ProgressBar>(R.id.progress_bar)
btn = findViewById<TextView>(R.id.text_view_button)

@@ -36,5 +29,12 @@ class SoundButton : FrameLayout {
})
}


fun move(col: Int, row: Int) {
val params = GridLayout.LayoutParams()
params.columnSpec = GridLayout.spec(col, 1, 1.0f)
params.rowSpec = GridLayout.spec(row, 1, 1.0f)
params.width = 0
params.height = 0
layoutParams = params
}
}

Loading…
取消
儲存