简体   繁体   中英

Kotlin drawer navigation with recyclerview

I'm coding a drawer navigation with recyclerview in Kotlin and I do not know how to fix several errors in my code. First I have to have a recyclerview, in order to scroll the fragments and each fragment has to change the color when it's active. My problem is that The fragments do not change on click, Home fragment is displaying all the time and even the inactive fragments look like active after clicking (if the fragment is active it's text color has to be red, otherwise - white).Also the items are stuck at the top and I cannot fix that. Here is my code. Adapter:

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>

The first question is where are you trying to add a new fragment? The second is, why are you assigning currentItem to be adapterPosition and then you are checking them? I guess, you are having RED fragment always, because currentItem and adapterPosition are equal all the time.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM