我正在尝试在Kotlin中实现Mike Penz的NavigationDrawer(https://github.com/mikepenz/MaterialDrawer)的部分内容。从那以后,我遇到了一些问题,主要是运营商。以下是实例化抽屉本身的代码的一部分。 Android Studio不会抛出任何错误,除非我在int和Long变量上使用==运算符:
// Create the Drawer
result = DrawerBuilder()
.withSliderBackgroundColor(ContextCompat.getColor(applicationContext, R.color.top_header))
.withActivity(this)
.withToolbar(toolbar)
.withHasStableIds(true)
.withItemAnimator(AlphaCrossFadeAnimator())
.withAccountHeader(headerResult!!)
.addDrawerItems(
PrimaryDrawerItem().withName(R.string.drawer_item_profile).withIcon(FontAwesome.Icon.faw_user).withIdentifier(1).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer_item_create).withIcon(FontAwesome.Icon.faw_paint_brush).withIdentifier(2).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer_item_yaanich_news).withIcon(FontAwesome.Icon.faw_newspaper_o).withIdentifier(3).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer_item_my_groups).withIcon(FontAwesome.Icon.faw_users).withIdentifier(4).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(5).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke))
)
.withOnDrawerItemClickListener { view, position, drawerItem ->
if (drawerItem != null) {
var intent: Intent? = null
if (drawerItem.identifier == (1) {
intent = Intent(this, UserProfileActivity::class.java)
} else if (drawerItem.identifier == 2) {
intent = Intent(this, YeetActivity::class.java)
} else if (drawerItem.identifier == 3) {
intent = Intent(this, RssActivity::class.java)
} else if (drawerItem.identifier == 4) {
intent = Intent(this, GroupsActivity::class.java)
} else if (drawerItem.identifier == 5) {
intent = Intent(this, UserSettingsActivity::class.java)
}
if (intent != null) {
this.startActivity(intent)
}
}
false
}
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(true)
.build()
RecyclerViewCacheUtil<IDrawerItem<*, *>>().withCacheSize(2).apply(result!!.recyclerView, result!!.drawerItems)
if (savedInstanceState == null) {
result!!.setSelection(21, false)
headerResult!!.activeProfile = profile
}
}
错误:
if (drawerItem.identifier == (1)
if (drawerItem.identifier == 2)
Operator == cannot be applied to 'Long and' 'Int'
只需在右侧使用很长时间即可
if (drawerItem.identifier == 1L)
编辑:这是必要的原因是Kotlin不会将Ints推广到Longs(或者更常见的是,不会扩展类型);在左侧我们有一个Long,在右侧我们有一个Int,导致错误。明确指示右侧是Long修复错误。
为了兴趣,另一个解决方案是使用compareTo()
。如果值相等,compareTo返回零,如果小于另一个则返回负数,如果大于另一个则返回正数:
if(drawerItem.identifier.compareTo(1) == 0) "Equals"
如果你遇到<operator!=不适用于long和int>的问题解决它只需在你的右边使用long即值!= 1L就是这样...