【发布时间】:2020-06-08 16:42:04
【问题描述】:
我正在使用 Kotlin 中的 recyclerview 编写抽屉导航,但我不知道如何修复代码中的几个错误。首先,我必须有一个 recyclerview,以便滚动片段,并且每个片段必须在其处于活动状态时更改颜色。我的问题是片段在点击时不会改变,主页片段一直在显示,即使是非活动片段在点击后看起来也很活跃(如果片段是活动的,它的文本颜色必须是红色,否则 - 白色)。还有物品卡在顶部,我无法解决。这是我的代码。 适配器:
class DrawerMenuAdapter(private val items: MutableList<DrawerMenuItem>) :
RecyclerView.Adapter<DrawerMenuAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.drawer_item_layout,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
return holder.onBind()
}
override fun getItemCount() = items.size
private lateinit var model: DrawerMenuItem
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onBind() {
var currentItem: Int
model = items[adapterPosition]
itemView.icon.setImageResource(model.icon)
itemView.description.text = model.description
itemView.setOnClickListener{
currentItem = adapterPosition
if (adapterPosition == currentItem){
itemView.description.setTextColor(Color.RED)
}
else{
itemView.description.setTextColor(Color.WHITE)
}
}
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main">
<FrameLayout
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"/>
</FrameLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
抽屉菜单项:
class DrawerMenuItem(val icon:Int, val description:String)
drawer_item_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:src="@drawable/ic_menu_gallery"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/description"
android:text="@string/app_name"/>
</LinearLayout>
【问题讨论】:
标签: android kotlin android-recyclerview navigation-drawer