如何解决StaggeredGridLayoutManager
我在代码中使用了 StaggeredGridLayoutManager
。
先看看这个:
我面临以下问题:
在第一次尝试中,我将瓷砖“1”直接向上移动一步,然后停止。一切看起来都不错,标记为“1”的磁贴替换了标记“2”。
在第二次尝试中,我将标有“3”的图块向上拖动但没有停止。它仍然会自动被 '1' 替换,它就在 '3' 上方。这里有问题。
在我的第 3 次和第 4 次尝试中,我尝试向上和横向拖动我的磁贴。一旦我这样做了,瓷砖会自动右移并移出框架(即使我的触摸仍在握住它)。只有当我向左拖动它时才能看到它。奇怪!
现在看看我的代码
在我的 MainActivity 的 onCreateMethod
val layoutmanager = StaggeredGridLayoutManager(if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) 3
else 2,StaggeredGridLayoutManager.VERTICAL)
layoutmanager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
recyclerView.layoutManager = layoutmanager
noteAdapter = NoteAdapter(myList,this)
recyclerView.adapter = noteAdapter
我的itemtouchhelperCallback
方法
private val itemtouchhelperCallback =
object :
itemtouchhelper.SimpleCallback(0,0
) {
override fun isLongPressDragEnabled(): Boolean {
return true
}
override fun isItemViewSwipeEnabled(): Boolean {
return true
}
override fun getMovementFlags(recyclerView: RecyclerView,viewHolder: RecyclerView.ViewHolder): Int {
val dragFlags = itemtouchhelper.UP or itemtouchhelper.DOWN or
itemtouchhelper.LEFT or itemtouchhelper.RIGHT
val swipeFlags = itemtouchhelper.START or itemtouchhelper.END
return makeMovementFlags(dragFlags,swipeFlags)
}
override fun onMove(
recyclerView: RecyclerView,viewHolder: RecyclerView.ViewHolder,target: RecyclerView.ViewHolder
): Boolean {
val fromPosition = viewHolder.adapterPosition
val toPosition = target.adapterPosition
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(myList,i,i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(myList,i - 1)
}
}
noteAdapter.notifyItemmoved(fromPosition,toPosition)
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder,direction: Int) {
}
}
更新
@Zain 发现真正的问题隐藏在我的 NoteAdapter.java
中,所以我强调了错误的方法。检查他的回答。
@Override
public int getItemViewType(int position) {
return position;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。