使用ItemTouchHelper

如何解决使用ItemTouchHelper

我有一个奇怪的错误,当我开始在其中拖动项目时,我的RecyclerView会滚动回到顶部位置。如果有任何区别,它位于ViewPager内部。您可以在.gif附件中看到该行为。

编辑:

调用RecyclerView时,notifyItemmoved视图似乎滚动到顶部,并且它的滚动程度与第一视图至少部分地显示在屏幕上一样。

enter image description here

查看

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/accounts_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarStyle="outsideOverlay"
    android:scrollbars="none"
    app:layoutManager="androidx.recyclerview.widget.linearlayoutmanager"
    tools:listitem="@layout/view_account_list_item" />

适配器

class Accountlistadapter(
private val onAccountClickListener: OnAccountClickListener) :
listadapter<Account,Accountlistadapter.ViewHolder>(
    AccountDiffCallback()
) {

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

override fun getItemId(position: Int): Long {
    return getItem(position).accountId.toLong()
}

override fun onBindViewHolder(holder: ViewHolder,position: Int) {
    holder.bind(getItem(position),onAccountClickListener)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),OnItemDragged {

    fun bind(account: Account,onAccountClickListener: OnAccountClickListener) {
        itemView.account_name.text = account.name
       
        itemView.setonClickListener {
            onAccountClickListener.onAccountClick(account)
        }

    }

    override fun onItemSelected() {
        itemView.setBackgroundColor(
            ContextCompat.getColor(
                itemView.context,R.color.background_contrast
            )
        )
    }

    override fun onItemClear() {
        itemView.setBackgroundColor(
            ContextCompat.getColor(
                itemView.context,R.color.background
            )
        )
    }

}

class AccountDiffCallback : DiffUtil.ItemCallback<Account>() {
    override fun areItemsTheSame(oldItem: Account,newItem: Account): Boolean {
        return oldItem.accountId == newItem.accountId
    }

    override fun areContentsTheSame(oldItem: Account,newItem: Account): Boolean {
        return (oldItem.balance == newItem.balance
                && oldItem.annualReturn == newItem.annualReturn
                && oldItem.name == newItem.name)
    }
}

interface OnAccountClickListener {
    fun onAccountClick(account: Account)
}

interface OnItemDragged {
    fun onItemSelected()
    fun onItemClear()
}}

itemtouchhelper

 private fun setuplistadapter() {

    accountlistadapter = Accountlistadapter(this)
    accountlistadapter.setHasstableIds(true)

    accounts_recycler_view.adapter = accountlistadapter
    accounts_recycler_view.addItemdecoration(
        DividerItemdecoration(
            requireContext(),DividerItemdecoration.VERTICAL
        )
    )


    val accountTouchHelper = itemtouchhelper(
        object : itemtouchhelper.SimpleCallback(
            itemtouchhelper.UP or itemtouchhelper.DOWN,0
        ) {

            override fun onSelectedChanged(
                viewHolder: RecyclerView.ViewHolder?,actionState: Int
            ) {
                if (actionState == itemtouchhelper.ACTION_STATE_DRAG) {
                    val accountViewHolder = viewHolder as Accountlistadapter.ViewHolder
                    accountViewHolder.onItemSelected()
                }
                super.onSelectedChanged(viewHolder,actionState)
            }

            override fun clearView(
                recyclerView: RecyclerView,viewHolder: RecyclerView.ViewHolder
            ) {
                val accountViewHolder = viewHolder as Accountlistadapter.ViewHolder
                accountViewHolder.onItemClear()
                super.clearView(recyclerView,viewHolder)
            }


            override fun onMove(
                recyclerView: RecyclerView,viewHolder: RecyclerView.ViewHolder,target: RecyclerView.ViewHolder
            ): Boolean {
                val fromPos: Int = viewHolder.adapterPosition
                val toPos: Int = target.adapterPosition
                Collections.swap(_accountList,fromPos,toPos)
                accountlistadapter.notifyItemmoved(fromPos,toPos)
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder,direction: Int) {}

        })

    accountTouchHelper.attachToRecyclerView(accounts_recycler_view)

}

解决方法

问题是我的RecyclerviewViewPager内部,而ConstraintLayoutmatch_parent内部。视图分页器在垂直方向上受到限制,高度设置为0dp,但是宽度设置为setHasFixedSize = true。我所需要做的是将宽度水平设置为0dp,将RecyclerView设置为notifyItemMoved

在调用RecyclerView作为适配器时,如果const User = require("../models/user"); exports.signup = (req,res) => { const user = new User(req.body); user.save((err,user) => { if(err || !user){ return res.status(400).json({ err: "NOT able to save user in DB" }) } res.json({ name: user.name,email: user.email,id: user._id }); }); }; exports.signout = (req,res) => { res.json({ message: "User signout" }); }; 是灵活的,则将重绘所有项目,并且默认情况下,它集中在第一个项目上。

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?