The problem here is when I'm returning from my support fragment to home fragment back every time my items in the recycle viewer got doubled. Each time i am shifting fragment to fragment my recycle viewer item in the homefragment got doubled. But when, I reopen the app its all got corrected but when i click on another fragment and come back the item in recycle viewer in home fragment got doubled. Kindly help me
// **HomeFragment.kt**
package com.service.bookitapp
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_home.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class HomeFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
private val arrCategory = ArrayList<CategoryModel>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
arrCategory.add(CategoryModel(R.drawable.electrician,"Electrician"))
arrCategory.add(CategoryModel(R.drawable.plumber,"Plumber"))
arrCategory.add(CategoryModel(R.drawable.acservice,"AC Service"))
arrCategory.add(CategoryModel(R.drawable.carpentry,"Carpentry"))
arrCategory.add(CategoryModel(R.drawable.drop,"Pick up & Drop"))
arrCategory.add(CategoryModel(R.drawable.painting,"Painting"))
arrCategory.add(CategoryModel(R.drawable.waterfilter,"Water Filter Repair"))
arrCategory.add(CategoryModel(R.drawable.packer,"Pack and Move"))
recyclerView.layoutManager = GridLayoutManager(context,3)
val recyclerAdapter = context?.let { RecycleCategoryAdapter(it,arrCategory) }
recyclerView.adapter = recyclerAdapter
}
}
// **fragment_home.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAF9F6"
android:padding="5dp"
android:orientation="vertical"
tools:context=".HomeFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_user"
android:textColor="@color/teal_200"
android:textSize="28sp"
android:padding="8dp"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:src="@drawable/profile"
android:padding="4dp"
android:contentDescription="@string/app_name" />
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/catg"
android:textStyle="bold"
android:textColor="#6E16e8"
android:layout_alignParentStart="true"
android:textSize="22sp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="370dp"
android:id="@ id/recyclerCategory">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/offers"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textColor="#6E16e8"
android:textSize="22sp"/>
<HorizontalScrollView
android:layout_margin="8dp"
android:layout_width="350dp"
android:layout_height="200dp"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:src="@drawable/plumbingservice"
android:contentDescription="@string/elect" />
<ImageView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:contentDescription="@string/elect"
android:src="@drawable/cleanview" />
<ImageView
android:layout_width="360dp"
android:layout_height="wrap_content"
android:contentDescription="@string/elect"
android:src="@drawable/elecview" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
The followig are the images
CodePudding user response:
first solution, before add first item in list, call clear
arrCategory.clear()
second solution, populate your list in onCreate method of your fragment
CodePudding user response:
Have you tried moving this
private val arrCategory = ArrayList<CategoryModel>(),
locally inside onViewCreated()
?
like this
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
val arrCategory = ArrayList<CategoryModel>() // <-- like this?
arrCategory.add(CategoryModel(R.drawable.electrician,"Electrician")
...
...
...
or you can simply initialize the list like this, removing all of it out of the onCreate()
private val arrCategory = arrayListOf<CategoryModel>(
CategoryModel(R.drawable.electrician,"Electrician"),
CategoryModel(R.drawable.plumber,"Plumber"),
CategoryModel(R.drawable.acservice,"AC Service"),
CategoryModel(R.drawable.carpentry,"Carpentry"),
CategoryModel(R.drawable.drop,"Pick up & Drop"),
CategoryModel(R.drawable.painting,"Painting"),
CategoryModel(R.drawable.waterfilter,"Water Filter Repair"),
CategoryModel(R.drawable.packer,"Pack and Move")
)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
recyclerView.layoutManager = GridLayoutManager(context,3)
val recyclerAdapter = context?.let { RecycleCategoryAdapter(it,arrCategory) }
recyclerView.adapter = recyclerAdapter
}