带recyclerview的科特林抽屉导航

问题描述 投票:0回答:1

我正在用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>

DrawerMenuItem:

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
1个回答
1
投票

第一个问题是您要在哪里添加新片段?第二个原因是,为什么要将currentItem分配为adapterPosition,然后再检查它们呢?我猜想,您总是有RED片段,因为currentItem和adapterPosition一直都相等。

itemView.setOnClickListener {
    currentItem = adapterPosition
    if (adapterPosition == currentItem) {
        itemView.description.setTextColor(Color.RED)
    } else {
        itemView.description.setTextColor(Color.WHITE)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.