Recyclerview未正确绑定滚动/位置上的项目已更改

时间:2018-10-26 08:15:28

标签: android android-recyclerview

我已经在回收者视图中实现了卡片视图。每个卡片视图包含两个文本视图。在我的适配器类中,我编写了以下代码来根据其长度增加文本大小。

       fun bindItems(note: UserNotes)
    {

        if(note.noteTitle == "" || note.noteTitle == null)
        {
            noteTitle.visibility = View.GONE

            if(gridViewSelected) {

                when {

                    note.noteText!!.length in 1..25 -> {
                        noteText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30f)
                        noteText.setTypeface(noteText.typeface, Typeface.BOLD)
                        noteText.gravity = Gravity.CENTER
                        noteText.text = note.noteText
                    }

                    else -> noteText.text = note.noteText


                }
            }



        }
        else if(!(note.noteTitle == "" || note.noteTitle == null) &&
                !(note.noteText == null || note.noteText == "")) {

            noteText.visibility = View.VISIBLE
            noteTitle.visibility = View.VISIBLE

            noteTitle.text = note.noteTitle
            noteText.text = note.noteText

        }


        if(note.noteText == null || note.noteText == "")
        {
            noteText.visibility = View.GONE
            //noteTitle.text = note.noteTitle


            when {

                note.noteTitle!!.length in 1..15 -> {

                    noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 23f)
                    noteTitle.gravity = Gravity.CENTER
                    noteTitle.text = note.noteTitle

                }

                else -> noteTitle.text = note.noteTitle


            }
        }
        else if(!(note.noteTitle == "" || note.noteTitle == null) &&
                !(note.noteText == null || note.noteText == "")) {

            noteText.visibility = View.VISIBLE
            noteTitle.visibility = View.VISIBLE

            noteTitle.text = note.noteTitle
            noteText.text = note.noteText

        }


    }

问题是,每当我滚动时,总会有一张或多张卡片缺少文本视图。这是图片:

enter image description here

如您所见,卡上缺少文本视图。我不知道我尝试粘贴文本视图的方式是否有效。有更好的替代方法吗?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

要解决您的问题。使用下面的类,并按如下所示调用GridSpacingItemDecorator.java

recyclerview = findViewById(R.id.recyclerview_ID)
recyclerview.addItemDecoration(GridSpacingItemDecorator(10))
  

GridSpacingItemDecorator.java

class GridSpacingItemDecorator(val spacing : Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
    if (outRect != null && view != null && parent != null) {
        val gridItemData = extractGridData(parent, view)
        val spanCount = gridItemData.spanCount
        val spanIndex = gridItemData.spanIndex
        val spanSize = gridItemData.spanSize
        outRect.left = (spacing * ((spanCount - spanIndex) / spanCount.toFloat())).toInt()
        outRect.right = (spacing * ((spanIndex + spanSize) / spanCount.toFloat())).toInt()
        outRect.top = spacing
    }
}

private fun extractGridData(parent: RecyclerView, view: View): GridItemData {
    val layoutManager = parent.layoutManager
    return if (layoutManager is GridLayoutManager) {
        extractGridLayoutData(layoutManager, view)
    } else if (layoutManager is StaggeredGridLayoutManager) {
        extractStaggeredGridLayoutData(layoutManager, view)
    } else {
        throw UnsupportedOperationException("Bad layout params")
    }
}

private fun extractGridLayoutData(layoutManager: GridLayoutManager, view: View): GridItemData {
    val lp = view.layoutParams as GridLayoutManager.LayoutParams
    return GridItemData(
            layoutManager.spanCount,
            lp.spanIndex,
            lp.spanSize
    )
}

private fun extractStaggeredGridLayoutData(layoutManager: StaggeredGridLayoutManager, view: View): GridItemData {
    val lp = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
    return GridItemData(
            layoutManager.spanCount,
            lp.spanIndex,
            if (lp.isFullSpan) layoutManager.spanCount else 1
    )
}

private class GridItemData (val spanCount: Int, val spanIndex: Int, val spanSize: Int)

}

答案 1 :(得分:0)

使用StaggeredGridLayoutManager而不是GridLayoutManager