如何解决使用ItemTouchHelper
我有一个奇怪的错误,当我开始在其中拖动项目时,我的RecyclerView
会滚动回到顶部位置。如果有任何区别,它位于ViewPager
内部。您可以在.gif附件中看到该行为。
编辑:
当调用RecyclerView
时,notifyItemmoved
视图似乎滚动到顶部,并且它的滚动程度与第一视图至少部分地显示在屏幕上一样。
查看
<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()
}}
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)
}
解决方法
问题是我的Recyclerview
在ViewPager
内部,而ConstraintLayout
在match_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 举报,一经查实,本站将立刻删除。