微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

StaggeredGridLayoutManager 列表滚动时的奇怪效果

如何解决StaggeredGridLayoutManager 列表滚动时的奇怪效果

我必须实现像 pinterest 那样的图像列表(行不对称的网格),并且我在片段中以这种方式使用了 StaggeredGridLayoutManager

场景片段

scenarioAdapter = Adapter(mutablelistof()) {
            with(requireActivity() as MainActivity) {
                startimageActivity(it.imageLink,it.name)
            }
        }
        scenarioAdapter.clearList()
        val mLayoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        mLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        scenario_list.layoutManager = mLayoutManager
        scenario_list.adapter = scenarioAdapter

        scenarioviewmodel.getScenario()

由于此图像列表已分页,因此在我的 onViewCreated 中,我称之为方法

private fun initScrollListener() {
        scenario_list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView,dx: Int,dy: Int) {
                super.onScrolled(recyclerView,dx,dy)
                val layoutManager = recyclerView.layoutManager as StaggeredGridLayoutManager
                layoutManager.invalidateSpanAssignments()
                val lastVisibleItemPositions = intArrayOf(0,0)
                layoutManager.findLastVisibleItemPositions(lastVisibleItemPositions)
                if ((lastVisibleItemPositions[1] == scenarioAdapter.itemCount - 1) &&
                    scenarioAdapter.itemCount >= BuildConfig.PAGE_SIZE && keepScrolling && !alreadyRefreshed) {
                    alreadyRefreshed = true
                    scenarioviewmodel.getScenario(scenarioAdapter.getPage())
                }
            }
        })
    }

它工作正常,可以正确加载图像。

这是适配器

 class Adapter(
        private val items: MutableList<ScenarioModel>,private val click: (ScenarioModel) -> Unit
    ) :
        RecyclerView.Adapter<Adapter.ViewHolder>() {

        fun update(list: MutableList<ScenarioModel>) {
            this.items.addAll(list)
            notifyDataSetChanged()
        }

        fun clearList() = items.clear()

        fun getPage(): Int = (items.size / BuildConfig.PAGE_SIZE)

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

        override fun onBindViewHolder(holder: ViewHolder,position: Int) {
            holder.bindItem(items[position],click)
        }

        override fun getItemCount(): Int = items.size

        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            fun bindItem(item: ScenarioModel,click: (ScenarioModel) -> Unit) {
                GlideApp.with(itemView.context).load(item.imageLink).into(itemView.scenario_image)
                itemView.setonClickListener { click(item) }
            }
        }
    }

问题是:当显示片段时,我显示了 10 个项目。当我向下滚动时,它完美地工作。但是当我回去,向上滚动时,这些项目会做一个动画,就像它们被重新排序或重新加载一样,它每次都会改变。谁能解决这个问题?

PS:问题与分页无关,我试图只获取第一页,尺寸很大,但问题仍然存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。