我发现了如何使用预览列表项
tools:listitem="@layout/my_item_layout"
Run Code Online (Sandbox Code Playgroud)
但android studio正在将recyclerview预览为垂直列表.有没有办法告诉Android Studio我会附加一个水平,LinearLayoutManager以便它可以预览水平列表?
我正在尝试使用androidX espresso库通过工具测试来测试我的UI。在我的年级,我有:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
compileSdkVersion 28
defaultConfig {
applicationId "it.zehus.mybike"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
android.defaultConfig.manifestPlaceholders = ['appAuthRedirectScheme': 'net.openid.appauthdemo']
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// room persistence library
def room_version = "2.1.0-alpha04"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // use …Run Code Online (Sandbox Code Playgroud) 我试图在 kotlin 的对象列表中找到一个值,使用它“过滤器”,但如果找到该值,我需要返回 true 或 false,但过滤器在匹配的情况下返回一个对象列表.
t.filter { it.retailerId == value }
Run Code Online (Sandbox Code Playgroud)
¿如何在对象列表中找到此值时返回布尔值?
我有两个活动,一个包含登录过程的所有片段,另一个包含主应用程序的所有片段。
假设我想从 Activity1(包含登录的所有导航图)导航到 Activity2(包含主应用程序的所有导航图)
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
fun goToMainActivity(){
startActivity(Intent(this,MainActivity::class.java))
finish()
}
}
Run Code Online (Sandbox Code Playgroud)
这里我调用方法 goToMainActivity()
class LoginFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_login,container,false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_go.setOnClickListener {
// call the method goToMainActivity() to kill all fragments contained by that Activity and move foward to MainActivity with another nav_graph
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个带有边框按钮的布局,如下所示(预期行为)。从 Material design 文档中,我读到了 Outlined Material Button,它似乎非常适合我的目的。我在布局中定义了按钮,提供了笔触宽度和笔触颜色,但没有显示任何边框,我做错了什么?
<com.google.android.material.button.MaterialButton
android:id="@+id/material_text_button"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Change name"
android:background="@android:color/transparent"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/listTextView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/listTextView2"
app:layout_constraintTop_toTopOf="@+id/listTextView2"
app:strokeColor="@color/green"
app:strokeWidth="10dp" />
Run Code Online (Sandbox Code Playgroud)
预期行为:
当前行为:
我试图在我的仪器测试的定义中使用反引号`。我不明白为什么编译器会抱怨一个奇怪的错误:
Caused by: com.android.tools.r8.internal.Jj: com.android.tools.r8.internal.Jc: Space characters in SimpleName 'given a closebottomsheetevent eventlistener onCloseBottomSheet should be called' are not allowed prior to DEX version 040
Run Code Online (Sandbox Code Playgroud)
我的测试是一个非常标准的仪器测试compose
Caused by: com.android.tools.r8.internal.Jj: com.android.tools.r8.internal.Jc: Space characters in SimpleName 'given a closebottomsheetevent eventlistener onCloseBottomSheet should be called' are not allowed prior to DEX version 040
Run Code Online (Sandbox Code Playgroud)
如果我删除反引号和空格测试编译没有任何问题:(
我正在与一个两次触发的 LiveData 观察者作斗争。在我的片段中,我观察LiveData如下,使用viewLifeCycleOwnerasLifeCycleOwner
private lateinit var retailViewModel: RetailsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retailViewModel = ViewModelProviders.of(this).get(RetailsViewModel::class.java)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
retailViewModel.retailLiveData.observe(viewLifecycleOwner, Observer {
// updating UI here, but firing twice!
}
retailViewModel.getRetailById(retail.id)
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图模型:
class RetailsViewModel(override val service: MyFoodyApiService = MyFoodyApiService.service) :
BaseViewModel(service) {
var retailLiveData: MutableLiveData<Retail> = MutableLiveData()
fun getRetailById(id: Int) {
scope.launch {
try {
val response =
service.getRetailById(authString, id).await()
when (response.isSuccessful) {
true -> {
response.body()?.let { …Run Code Online (Sandbox Code Playgroud) 编译器告诉我 setColorFilter 已被弃用!
tab?.icon?.setColorFilter(
ContextCompat.getColor(requireActivity(), R.color.colorLogoGrey),
PorterDuff.Mode.SRC_IN
)
Run Code Online (Sandbox Code Playgroud) 尝试使用从异步方法调用的挂起函数来实现对错误的优雅处理,如何捕获挂起方法引发的错误。
suspend fun findById(id: Long): User? {
throw Exception("my exception") // intentionally throwing to simulate error situation.
return userModel.findById(id) // IO, may throw an error
}
Run Code Online (Sandbox Code Playgroud)
调用者部分,通过 IO 线程启动
GlobalScope.launch(Dispatchers.IO) {
try {
var userAsync: Deferred<User?>? = null
arguments?.getLong("id")?.let {
userAsync = async { viewModel?.findById(it) } // async for efficiency as i've other async methods too.
}
val data = userAsync?.await()
withContext(Dispatchers.Main) {
user = data // data binding, populating UI fields of user
}
} catch (exception: Exception) { …Run Code Online (Sandbox Code Playgroud) 在我的 flutter 应用程序中,我有这样的配置:
api_url = https://api.poemhub.top
Run Code Online (Sandbox Code Playgroud)
问题是,在不同的环境下api url是不同的,在开发时,api应该是这样的:
api_url = https://dev-api.poemhub.top
Run Code Online (Sandbox Code Playgroud)
在测试环境中:
api_url = https://beta-api.poemhub.top
Run Code Online (Sandbox Code Playgroud)
在生产环境中:
api_url = https://api.poemhub.top
Run Code Online (Sandbox Code Playgroud)
不同地方的其他配置也不同,那么在不同环境中处理此配置的最佳方法是什么?我已经阅读了一个定义不同main.dart并运行构建命令的解决方案,如下所示:
flutter build apk -t lib/main_develop.dart
Run Code Online (Sandbox Code Playgroud)