kotlin 从片段返回时,Recycleviewer项加倍

i34xakig  于 2022-11-16  发布在  Kotlin
关注(0)|答案(2)|浏览(124)

这里的问题是,当我从我的支持片段返回到主片段时,每次我在回收查看器中的项目都被加倍了。每次我将片段转移到片段时,我在主片段中的回收查看器项目都被加倍了。但当我重新打开应用程序时,它的所有内容都得到了纠正,但当我单击另一个片段并返回时,主片段中的回收查看器中的项目都被加倍了。请帮助我

// **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>

以下是图像

ybzsozfc

ybzsozfc1#

第一个解决方案,在列表中添加第一项之前,调用clear

arrCategory.clear()

第二个解决方案,在片段onCreate方法中填充列表

46qrfjad

46qrfjad2#

你试过移动这个吗

private val arrCategory = ArrayList<CategoryModel>(),

onViewCreated()内部局部地?
就像这样

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")            
    ...
    ...
    ...

也可以像这样初始化列表,将其全部从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
}

相关问题