Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List now scrolls down, left and right on dpad interaction #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ plugins {
}

android {
compileSdk = 30
compileSdk = 31
buildToolsVersion = "30.0.3"

defaultConfig {
applicationId = "dev.berggren"
minSdk = 26
targetSdk = 30
targetSdk = 31
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -55,7 +55,8 @@ dependencies {
implementation("androidx.compose.material:material:${rootProject.extra["compose_version"]}")
implementation("androidx.compose.ui:ui-tooling:${rootProject.extra["compose_version"]}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
implementation("androidx.activity:activity-compose:1.3.0-beta02")
implementation("androidx.activity:activity-compose:1.4.0")
implementation("androidx.compose.foundation:foundation:1.2.0-alpha01")
testImplementation("junit:junit:4.+")
androidTestImplementation("androidx.test.ext:junit:1.1.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/dev/berggren/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
@ExperimentalFoundationApi
@ExperimentalComposeUiApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
27 changes: 25 additions & 2 deletions app/src/main/java/dev/berggren/ScrollableGrid.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.berggren

import android.view.KeyEvent
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.unit.dp

@ExperimentalFoundationApi
@ExperimentalComposeUiApi
@Composable
fun <T> ScrollableGrid(
Expand All @@ -21,15 +24,35 @@ fun <T> ScrollableGrid(
.verticalScroll(verticalScrollState)
) {
items.forEach { rowItems ->

val rowScrollState = remember { ScrollState(initial = 0) }

Row(
Modifier
.fillMaxWidth()
.padding(bottom = 24.dp)
.horizontalScroll(rowScrollState)
) {
rowItems.forEach { rowItem ->
Row {

rowItems.forEachIndexed { rowItemIndex, rowItem ->
Row(
modifier = Modifier
.onKeyEvent {
it.nativeKeyEvent
var bool = false
if (it.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) {
if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (rowItemIndex == 0)
bool = true
}
if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (rowItemIndex == rowItems.count() - 1)
bool = true
}
}
bool
}
) {
Box {
contentForItem(rowItem)
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.0-beta05")
classpath("com.android.tools.build:gradle:7.0.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10")

// NOTE: Do not place your application dependencies here; they belong
Expand Down