我有 2 个视图,并且需要在下方设置一个屏障,但该屏障无法按预期工作。这是我的布局。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="This is a text view"
app:layout_constraintEnd_toStartOf="@+id/t1"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/t1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView15"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a demo text to check wrap content"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.constraintlayout.widget.Barrier
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="textView15,t1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
黑色虚线是屏障。
这可能是一个错误或者我做错了,预览和实际设备中的结果是相同的
xml android android-layout android-constraintlayout constraintlayout-barrier
我正在嵌套启动{}的流程中从数据存储区收集数据。
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
launch {
DataStore.userName.collect {
// it emits string value
Log.e(TAG, it )
}
}
launch {
DataStore.userPhone.collect {
// it emits string value
Log.e(TAG, it )
}
}
launch {
DataStore.userAddress.collect {
// it emits string value
Log.e(TAG, it )
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来收集碎片中的流量?就像在单个 launch{} 块中收集所有数据一样。
我正在使用房间数据库和数据存储。这是我的代码
视图模型
@HiltViewModel
class SubscriptionViewModel @Inject constructor(
private val userDataStore: UserDataStore,
private val walletDao: WalletDao
) : ViewModel() {
val flow: Flow<List<Transaction>> = walletDao.getAllTransactions(userDataStore.getId())
// This shows an error as userDataStore.getId() is a suspend function in DataStore
init {
viewModelScope.launch(IO) {
getWalletAmount()
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据存储
suspend fun getId(): String {
val preferences = dataStore.data.first()
return preferences[USER_ID] ?: ""
}
Run Code Online (Sandbox Code Playgroud)
钱包道
@Query("SELECT * FROM recent_transactions where user_id=:user_id")
fun getAllTransactions(user_id: String): Flow<List<Transaction>>
Run Code Online (Sandbox Code Playgroud)
问题是我无法初始化流程。我尝试使用 Lateinit 在 init{ } 块中初始化它,但是在片段中访问时它崩溃,表示流程尚未初始化。
任何解决方案都会有帮助。