假设我有以下数据结构:
Map<String, ObjectA> map;
Run Code Online (Sandbox Code Playgroud)
和ObjectA:
class ObjectA {
String a;
String b;
int c;
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是回归:
Map<String, String>
Run Code Online (Sandbox Code Playgroud)
其中第一个String与原始String相同,第二个String是'String a'.我需要做些什么改造呢?
如果这是一个列表,我会使用Observable.from(),从单个项目中获取我想要的东西,然后在最后将它们与toList()连接在一起.但这是一张地图,我从来没有也不知道如何在RxJava中对地图进行迭代.任何帮助,将不胜感激
编辑.我知道我可以使用Observable.create来执行此操作并使用正常的Java /方式来完成它,就像第一个回答状态一样.我真正想知道的是,是否有任何Rx操作符允许我这样做,如Observable.from,flatMap和toList,如果我正在转换列表
出于某种原因,我有时想使用RxOperators而不是普通的java方式转换数据结构,因为它更干净,更清洁.例如:
Observable.from(listOfStrings)
.filter(string -> string.getNumber() >5)
.toList()
Run Code Online (Sandbox Code Playgroud)
有没有办法等待observable的结果并在函数中返回:这样做(但它不起作用):
private String getFilteredString(String string){
Observable.from(listOfStrings)
.filter(string -> string.getNumber() >5)
.toList()
.subscribe(finalStirng -> {
return finalString;
})
}
Run Code Online (Sandbox Code Playgroud) 所以在java中我们有三元运算符(?),它有时很容易通过if-else内联计算一些值.例如:
myAdapter.setAdapterItems(
textToSearch.length == 0
? noteList
: noteList.sublist(0, length-5)
)
Run Code Online (Sandbox Code Playgroud)
我知道kotlin中的等价物是:
myAdapter.setAdapterItems(
if(textToSearch.length == 0)
noteList
else
noteList.sublist(0, length-5)
)
Run Code Online (Sandbox Code Playgroud)
但我曾经习惯于喜欢Java中的三元运算符,用于短表达式条件,以及将值传递给方法时.有没有Kotlin等价物?
Kotlin是否支持java注释@ColorInt,因为我无法使其工作.我可以用一个颜色注释@ColorInt,但实际上并没有得到该注释的lint检查.
例
fun setStatusBarColor(@ColorInt color){
window.setStatusBarColor(color)
}
Run Code Online (Sandbox Code Playgroud)
通过时R.color.colorPrimary,不显示棉绒.应该显示lint R.color.colorPrimary和不显示resources.getColor(R.color.colorPrimary)
我不知道为什么会发生这种情况,但调用notifyItemInserted(0)(仅限第一个位置)不会为视图设置动画.在其他位置一切正常.
在这种情况下工作的动画:
friendsList.remove(positionFriend);
friendsList.add(1, newFriend);
notifyItemInserted(1);
notifyItemRemoved(positionFriend+1);
Run Code Online (Sandbox Code Playgroud)
动画在这种情况下不起作用:
friendsList.remove(positionFriend);
friendsList.add(0, newFriend);
notifyItemInserted(0);
notifyItemRemoved(positionFriend+1);
Run Code Online (Sandbox Code Playgroud)
预期行为:元素插入顶部并插入动画.
发生了什么:没有显示插入动画,实际上我认为'视觉',第一个元素消失,移动动画发生.
使用coordinatorLayout将滚动行为添加到布局时,如下所示:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
mainContent是重要的部分.
真正的布局将在此容器内膨胀.
想象我View由一个RecyclerView,并在屏幕的底部有固定的布局.
有人知道一种方法来删除底部固定布局的滚动行为并保持RecyclerView- Toolbar隐藏行为吗?
在Kotlin中,此代码的语法是否更短:
if(swipeView == null){
swipeView = view.find<MeasureTypePieChart>(R.id.swipeableView)
}
Run Code Online (Sandbox Code Playgroud)
首先我试过这个:
swipeView ?: view.find<MeasureTypePieChart>(R.id.swipeableView)
Run Code Online (Sandbox Code Playgroud)
但后来我意识到这不是一个任务,所以代码什么都不做.然后我试过:
swipeView = swipeView ?: view.find<MeasureTypePieChart>(R.id.swipeableView)
Run Code Online (Sandbox Code Playgroud)
哪个有效,但有点冗长.我希望这样的事情:
swipeView ?= view.find<MeasureTypePieChart>
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这不起作用.有没有办法用短语法完成这个?
我知道我可以这样做:
variable?.let { it = something } which works.
Run Code Online (Sandbox Code Playgroud) /**对于 Java,我们过去常常在方法之前使用,然后就会出现一个漂亮的自动生成的文档。不能用 Kotlin 做同样的事情。此功能是否可用,默认情况下处于关闭状态?我怎样才能打开它?
所以我有以下场景:
class NowActivity: AppCompatActivity(), NowScreen, NowDelegate by NowDelegateImpl(){
onCreate(...){
presenter.attachView(this)
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将某些NowScreen方法的实现委托给 NowDelegate 以便我可以在演示者内部执行以下操作:
view.callSomeFunc()
Run Code Online (Sandbox Code Playgroud)
其中 callSomeFund() 在NowDelegate.
有没有办法完成这样的事情?问题是我正在使用 MVP,它将视图附加到演示者。但是一些视图实现在几个活动中重复,所以我想将它委托给另一个类。
所以我有一个交互器,它使用 Realm 执行插入操作,然后通过 RealChangeListener 通知插入已完成。它是这样的:
fun insertCar(item: Car) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(...)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
fun insertCar(item: Car, listener: RealmChangeListener<Car>) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(listener)
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样访问:
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
})
Run Code Online (Sandbox Code Playgroud)
但后来我无法删除这个听众
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
it.removeChangeListener(this)
})
Run Code Online (Sandbox Code Playgroud)
this 将指向它所在的类而不是实际的侦听器
我也可以这样做:
fun insertCar(item: Car, doAfterChange (Car) -> Unit) {
realm.doInTransaction {
val car = Car(...)
val copy …Run Code Online (Sandbox Code Playgroud)