KotlinAndroid Studio Recyclerview不回收

htrmnn0y  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(172)

我只是做一些初学者的事情,我遵循一个简单的教程在Android Studio中创建一个回收视图,在Pixel 5 API 24上启动它它几乎完美,我在strings.xml中使用字符串数组实现了它。但主要的问题是,实际上回收功能不工作。如果我启动我的程序,recyclerview填充所有17个项目,但如果我向下滚动到最后一个项目,它不会回收所有列表,并重新启动项目0我不能解释这一点,我按照指南一步一步...谢谢你的耐心。这里是文件:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@drawable/cartoon_background">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:id="@+id/quest"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        app:cardElevation="16dp"
        android:layout_margin="10dp"
        android:padding="10dp"
        app:cardBackgroundColor="#FBEFCA"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="128dp"
                android:layout_height="128dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_weight="1"
                app:srcCompat="@drawable/craftsman" />

            <TextView
                android:id="@+id/title"
                android:textSize="32dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/icon"
                android:layout_alignParentTop="true"
                android:text="Craftsman" />

            <TextView
                android:id="@+id/description"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_toRightOf="@id/icon"
                android:text="Oh ohh ohhh! Pick that armour! You sould at least 100 items at craftsman." />

            <TextView
                android:id="@+id/lock"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/description"
                android:layout_marginLeft="4dp"
                android:layout_marginBottom="10dp"
                android:layout_toRightOf="@id/title"
                android:text="Unlocked"
                android:textSize="18dp" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

MainActivity.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.recyclerview.layoutManager = LinearLayoutManager(this)

        val data = ArrayList<ItemsViewModel>()
        val icons: IntArray = intArrayOf(R.drawable.craftsman, R.drawable.building, R.drawable.crown, R.drawable.excalibur, R.drawable.gold_boots, R.drawable.gold_cup, R.drawable.helmet,
        R.drawable.horse, R.drawable.knight, R.drawable.ring_temple, R.drawable.lord, R.drawable.secret_quest, R.drawable.sword_man, R.drawable.touch, R.drawable.wizard, R.drawable.tourch, R.drawable.workwoman)
        val titles = resources.getStringArray(R.array.list_title)
        val descriptions = resources.getStringArray(R.array.list_description)
        val locks = resources.getStringArray(R.array.list_lock)

        for (i in 0..16) {
            data.add(ItemsViewModel(icons[i], titles[i],descriptions[i], locks[i]))
        }

        val adapter = CustomAdapter(data)
        binding.recyclerview.adapter = adapter

        adapter.notifyDataSetChanged()

    }

}

CustomAdapter.kt

class CustomAdapter(private val mList: List<ItemsViewModel>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    class ViewHolder(binding: CardViewBinding) : RecyclerView.ViewHolder(binding.root) {
        val icon = binding.icon
        val description = binding.description
        val lock = binding.lock
        val title = binding.title
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = CardViewBinding.inflate(LayoutInflater.from(parent.context), parent,
                                                                false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val ItemsViewModel = mList[position]

        holder.icon.setImageResource(ItemsViewModel.icon)
        holder.title.text = ItemsViewModel.title
        holder.description.text = ItemsViewModel.description
        holder.lock.text = ItemsViewModel.lock
    }

}
brqmpdu1

brqmpdu11#

回收意味着它只会创建一些项目视图(在您的情况下是cardview项目),这些项目视图可以显示在屏幕上和滚动时,例如在向上滚动时,项目视图将隐藏在屏幕上方,相同的视图将重新使用更新的数据用于从下方输入的新项目。这可以通过在onCreateViewHolder方法中放置一个log语句来观察。它只会创建大约数量的viewholders,将覆盖屏幕,并将重复使用相同的。例如,如果8个项目将覆盖屏幕,它可以创建大约10个viewholders,并重新使用它们来显示整个列表,无论大小如何。
在你的情况下,你说不回收是什么意思?

相关问题