我是 Jetpack 的新手,尤其是 Compose,并且正在努力寻找一种方法来打开网站或通过单击位于 TopAppBar 中的 IconButton 来启动 Chrome 浏览器。我应该通过调用我可以编写的“linkToWebpage()”函数来执行此操作,还是简单地与 IconButton 的 onClick = {} 函数内联来执行此操作?我该怎么做?我正在使用导航库进行应用内导航,并取得了巨大成功,但在加载网页时遇到了困难。请注意,为了便于阅读,我省略了一些代码。感谢您的时间和帮助!
@Composable
fun HomeScreen() {
val navController = rememberNavController()
...
Scaffold(
topBar = {
TopAppBar(
title = {},
navigationIcon =
{
IconButton(onClick = { linkToWebpage() }) {
Icon(Icons.Filled.Favorite)
}
}
,
// TODO get appbar color from global theme.
backgroundColor = Color.DarkGray,
)
},
bottomBar = {
...
}
) {
NavHost(navController, startDestination = Screen.Courses.route) {
...
}
}
Run Code Online (Sandbox Code Playgroud) 我正在努力在 Android Jetpack Compose 中实现 DataStore 首选项库,以在我的应用程序中保留一些用户设置。每当我尝试从可组合组件访问 SettingsViewModel 时,应用程序就会崩溃并收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.boldmethod, PID: 5415
java.lang.RuntimeException: Cannot create an instance of class com.boldmethod.Models.SettingsViewModel
...
Caused by: java.lang.InstantiationException: java.lang.Class<com.packageName.Models.SettingsViewModel> has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)
我正在按照文档创建数据存储和视图模型,所以也许我没有在可组合项中正确使用它们。这是相关的源代码:
摇篮
dependencies {
// Kotlin/Core
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.20"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
// Compose
implementation "androidx.compose.ui:ui:1.0.0-alpha08"
implementation "androidx.compose.material:material:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime-livedata:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha08"
// Navigation
def nav_compose_version = "1.0.0-alpha03"
implementation "androidx.navigation:navigation-compose:$nav_compose_version"
// Architecture
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha05"
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
//implementation "android.arch.lifecycle:runtime:2.1.0"
// UI/Material
implementation 'com.google.android.material:material:1.2.1'
implementation …Run Code Online (Sandbox Code Playgroud) android android-viewmodel android-jetpack android-jetpack-compose android-jetpack-datastore
我正在尝试创建一个简单的新闻应用程序,但是当我@Inject constructor在片段中使用 of 时,导航组件不起作用并显示以下错误...而在使用注入之前,它的工作没有任何问题
我要做什么来修复这个错误?非常感谢你的帮助
新闻活动:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.simplenewsapp.R
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.activity_news.*
@AndroidEntryPoint
class NewsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_news)
bottomNavigationView.setupWithNavController(newsNavHostFragment.findNavController())
}
}
Run Code Online (Sandbox Code Playgroud)
突发新闻片段:
class BreakingNewsFragment @Inject constructor(
val newsItemAdapter: NewsAdapter,
var viewModel: MainViewModel? = null
) : Fragment(R.layout.fragment_breaking_news) {
...
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.simplenewsapp, PID: 8558
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplenewsapp/com.example.simplenewsapp.ui.NewsActivity}: android.view.InflateException: Binary XML file line #17: Binary XML …Run Code Online (Sandbox Code Playgroud) android dependency-injection android-fragments android-jetpack dagger-hilt
我需要你的帮助。
一切都在标题中,我不知道我该怎么做......请帮助我。我使用了很多东西,如补间、枚举类等,但没有用,我想要 200dp 的初始值,当活动打开时,2 秒后,图像在 2 秒内从 200dp 变为 100dp。谢谢。
我目前正在尝试 Android Jetpack Compose,并希望RecyclerView使用LazyColumn. 但是,当在用户滚动期间或在另一个编程滚动(动画)期间以编程方式滚动(动画)到某个项目时,不会呈现列表中的某些项目。这是 Compose 的问题还是我错误地实现了滚动?
作为一个简单的例子,可以使用以下代码:
class MainActivity : ComponentActivity() {
@ExperimentalTime
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val listState = rememberLazyListState()
rememberCoroutineScope().launch {
delay(10.seconds)
listState.animateScrollToItem(1000)
delay(100.milliseconds)
listState.animateScrollToItem(0)
}
LazyColumn(
state = listState,
contentPadding = PaddingValues(1.dp),
) {
items(1000) {
Text(
text = "$it",
modifier = Modifier
.padding(2.dp)
.clip(MaterialTheme.shapes.small)
.background(MaterialTheme.colors.onSurface)
.padding(8.dp)
.fillMaxWidth(),
color = MaterialTheme.colors.surface,
textAlign = TextAlign.Center,
)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还录制了该应用程序的示例视频: https: //i.imgur.com/dKFn1VH.mp4。
android android-jetpack android-jetpack-compose android-jetpack-compose-list
我试图在用户可以从欢迎页面显示欢迎消息时在仪表板上向用户显示欢迎消息。因此,在仪表板的路线中,我定义了一个可选参数。
无需任何参数即可访问它(因为它是可选的),但当我添加一个时,它就不再工作了。
composable(Routes.welcome) {
WelcomeScreen {
navController.navigate("dashboard?isWelcome={true}")
}
}
composable(
"dashboard?isWelcome={isWelcome}}",
arguments = listOf(
navArgument("isWelcome") {
type = NavType.BoolType
}
)
) {
val isFromWelcome = it.arguments?.getBoolean("isWelcome") ?: false
...
}
Run Code Online (Sandbox Code Playgroud)
我的错误是:
Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/dashboard?isWelcome={true} } cannot be found in the navigation graph
Run Code Online (Sandbox Code Playgroud)
谢谢你的建议 :)
android android-jetpack android-architecture-navigation android-jetpack-compose
我需要观察从 firebase 获取的列表的变化
viewModel.courseList.observe(viewLifeCycleOwner,{
})
Run Code Online (Sandbox Code Playgroud)
但我无法使用viewLifeCycleOwner.
我有一个项目列表,其中每个项目都有一个复选框。复选框的状态需要存储在 中,viewmodel并且最好中的列表viewmodel应该是唯一的事实来源。
@Composable
fun Screen(viewModel: ViewModel) {
val list by viewModel.items.observeAsState()
LazyColumn {
list?.let { items ->
items(items) { item ->
ListItem(
text = {
Text(item.name)
},
secondaryText = {
Text(item.phoneNumber)
},
icon = {
Checkbox(
modifier = Modifier.padding(8.dp),
checked = item.selected,
onCheckedChange = {
//TODO
}
)
}
)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试item.selected通过更新回调中的列表(MutableLiveData<List<Object>>in viewmodel)来更新onCheckedChange,但 UI 没有更新。如果我向下滚动然后向上滚动,用户界面会更新,并且该复选框似乎已被选中。为什么不更新?
android-livedata android-jetpack android-jetpack-compose lazycolumn
我正在尝试在我的项目中添加导航,但其显示未能添加导航依赖项,
当我在窗口中单击“确定”立即添加这些内容时,它显示添加依赖项失败,并且我的导航 xml 编辑器不可见
我的 build.gradle (应用程序):-
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
id 'kotlin-android-extensions'
}
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.choudhary.myshop"
minSdkVersion 21
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
androidExtensions {
experimental = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding = true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" …Run Code Online (Sandbox Code Playgroud) android-jetpack ×10
android ×9
kotlin ×5
android-architecture-navigation ×1
android-jetpack-compose-list ×1
dagger-hilt ×1
lazycolumn ×1