涵盖Android架构组件的最新样本之一是Google提供的GithubBrowserSample.我查看了代码并出现了一些问题:
我注意到ViewModelModule包含在AppModule中.这意味着所有视图模型都被添加到DI图中.为什么以这种方式完成而不是Module为每个只提供ViewModel特定活动/片段所需的活动/片段分开?
在这个具体的例子中,使用GithubViewModelFactory实例化视图模型有没有办法将参数传递给特定的ViewModel?或者更好的解决方案是创建一个setter ViewModel并通过setter设置所需的param?
android dagger dagger-2 android-viewmodel android-architecture-components
为了获得用户当前位置,我使用过LocationManager:
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
Run Code Online (Sandbox Code Playgroud)
它易于阅读且代码非常简单.
但我注意到Google最近在Google Play服务中发布了新客户端API模型并建议使用FusedLocationProviderApi,它看起来更复杂,它是异步的,它需要处理回调等.
在LocationManager上使用FusedLocationProviderApi有什么好处吗?
android android-location google-play-services android-maps-utils
我遵循 BLoC 模式并订阅流,并对构建方法中的状态变化做出反应。加载数据后,我想关闭屏幕。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bloc'),
),
body: SafeArea(
child: StreamBuilder<UserState>(
stream: _userBloc.user,
initialData: UserInitState(),
builder: (context, snapshot) {
if (snapshot.data is UserInitState) {
return _buildInit();
}
if (snapshot.data is UserDataState) {
Navigator.pop(context, true);
return Container();
}
if (snapshot.data is UserLoadingState) {
return _buildLoading();
}
},
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
当我Navigator.pop(context, true);使用build()方法时,我得到:
I/flutter ( 4360): ??? EXCEPTION CAUGHT BY ANIMATION LIBRARY ??????????????????????????????????????????????????????????
I/flutter ( 4360): The following …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用UIL将图像加载到位图.我需要加载多个图像,我注意到在某些情况下图像网址可能是相同的.在这种情况下,只加载第一张图像.如何避免在UIL中取消请求?
代码在循环中运行3次:
ImageSize targetSize = new ImageSize(70, 70);
ImageLoader.getInstance().loadImage("http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png", targetSize, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
Log.e("tag", "onLoadingStarted");
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
Log.e("tag", "onLoadingFailed");
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Log.e("tag", "onLoadingComplete");
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
Log.e("tag", "onLoadingCancelled");
}
});
Run Code Online (Sandbox Code Playgroud)
日志是:
onLoadingStarted
onLoadingStarted
onLoadingStarted
onLoadingComplete
onLoadingCancelled
onLoadingCancelled
Run Code Online (Sandbox Code Playgroud) Kotlin 协程中的 Dispatchers.Main 和 Dispatchers.Default 有什么区别?
我使用viewModelScope.launch {}并按预期启动块在 UI 线程上执行。然后我发现它默认为viewModelScope.launch(Dispatchers.Default) {}.
这让我有点困惑,因为我认为我应该用来Dispatchers.Main在 UI 线程上执行操作。
到目前为止,Android 上似乎Dispatchers.Default默认为Dispatchers.Main. 那正确吗?
如果我使用一种或另一种或者它们可以互换,是否有任何缺点?如果它们在 Android 上可以互换,如果将来我会添加对 kotlin 多平台的支持,是否会影响某些事情?
我正在使用事务在Firestore中实现post喜欢和评论功能.我使用事务是因为我需要在帖子中的likes/comments子集和更新计数器中添加新字段,并且还将post id添加到用户喜欢/评论的帖子集合中.
我注意到如果我离线了,我就像这样请求我的帖子一切正常:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
postDocRef.get().addOnSuccessListener { doc ->
val post = doc.toObject(Post::class.java)
Timber.e(post.toString())
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在事务中执行相同的异常抛出:
val postDocRef = FirebaseUtil.postsColRef.document(postId)
FirebaseUtil.firestore.runTransaction(Transaction.Function<Void> { transaction ->
val post = transaction.get(postDocRef).toObject(Post::class.java)
}
Run Code Online (Sandbox Code Playgroud)
例外情况是:
com.google.firebase.firestore.FirebaseFirestoreException:UNAVAILABLE
为什么离线模式在交易中不起作用?是否可以在离线状态下实现此功能(在子集合中添加条目并在不同对象中更新字段)?
用continueWithTask()调用链替换事务有什么缺点?
我已经开始使用Twitter Fabric for Android.用TwitterLoginButton登录工作正常,但在某些情况下我不需要使用TwitterLoginButton,我只需要获取用户令牌和秘密.代码看起来正确,打开了Twitter登录表单,但完全调用了回调.
TwitterAuthClient authClient = new TwitterAuthClient();
authClient.authorize(TwitterSettingsActivity.this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
Logger.e(TAG, "ok");
}
@Override
public void failure(TwitterException e) {
Logger.e(TAG, "failure error", e);
}
});
Run Code Online (Sandbox Code Playgroud)
我想我需要在onActivityResult中添加一些处理,但在文档https://dev.twitter.com/twitter-kit/android/request-email中没有关于它的信息
我有一个自定义视图,我需要调用一个特定的方法来打开一个活动.在浓缩咖啡测试中,这样做的正确方法是什么?我只需要膨胀这个视图或者我需要编写一个自定义的ViewAction?
通过示例,我看到了两种使用Android架构组件的MVVM方法.
第一种方法:
ViewModel 提供 LiveDataActivity 订阅 LiveDataActivity正在设置数据时ViewModel ObservableField.ViewModel传递给绑定.在xml你刚刚设置ObservableField为值
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:visibleGone="@{viewmodel.listLoading}"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:refreshing="@{viewmodel.listRefreshing}"
app:onRefreshListener="@{() -> viewmodel.refreshList()}"
app:visibleGone="@{!viewmodel.listLoading}">
Run Code Online (Sandbox Code Playgroud)优点:我并不需要通过状态(例如"加载"),因为我更新listLoading ObservableField在ViewModel为这样的:
val listLoading = ObservableBoolean(false)
/** other observable fields go here **/
val list: MutableLiveData<List<Item>> = MutableLiveData()
fun loadList() {
listLoading.set(true)
repo.getList { items ->
list.value = items
listLoading.set(false)
}
}
Run Code Online (Sandbox Code Playgroud)
缺点:这种方法有任何缺点吗?
第二种方法:
ViewModel 提供 LiveDataActivity …android design-patterns android-databinding android-mvvm android-architecture-components
我有Android开发背景,正在学习Flutter。
在Android中,通常的做法是使用Kotlin密封类从ViewModel返回状态,例如
sealed class MyState {
data class Success(val data: List<MyObject>) : MyState()
data class Error(val error: String) : MyState()
}
Run Code Online (Sandbox Code Playgroud)
我想在Flutter中使用类似的模式,并从BLOC类返回一个State对象。在Flutter中实现相同效果的最佳方法是什么?
要构建即时应用程序,需要设置应用程序链接.其中一个步骤需要验证您是要将应用链接到的网站的所有者.如果我没有任何网站但仍希望能够向我的朋友发送我的即时应用程序链接,在社交媒体上分享等,是否可以创建即时应用程序?
基本上,我们的想法是在应用程序中实现"共享"功能,该功能将生成将导致即时应用程序的链接.
我有一个WS,如果成功则返回200,没有任何正文,否则返回420,错误正文中的json不成功
返回类型为
Observable<Response<Void>>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,如果出现420代码错误,onNext(Response<Void> value)则调用onError(Throwable e)该代码,而不是针对其他任何不成功的请求调用该代码。
为什么仅在这种情况下才调用onNext而不是onError?如果请求未返回200,则可以调用onError?