【问题标题】:Kotlin drawer navigation with recyclerview带有 recyclerview 的 Kotlin 抽屉导航
【发布时间】: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


    【解决方案1】:

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

    itemView.setOnClickListener {
        currentItem = adapterPosition
        if (adapterPosition == currentItem) {
            itemView.description.setTextColor(Color.RED)
        } else {
            itemView.description.setTextColor(Color.WHITE)
        }
    }
    

    【讨论】:

    • 我想我只是在 recyclerview 中添加列表项而不是片段,这是一个问题
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多