gpt4 book ai didi

android - 使用 SmoothScroller 时一次可点击单个项目

转载 作者:行者123 更新时间:2023-11-29 23:28:58 41 4
gpt4 key购买 nike

我有以下 Github 示例:A sample to showcase contact list divided into sections with group header

在 Adapter 类中,当用户点击一个项目时,我有以下代码:

boolean expanded = contact.isExpanded();
contact.setExpanded(!expanded);
notifyItemChanged(getAdapterPosition());

if (contact.isExpanded()) {
mSmoothScroller.setTargetPosition(getAdapterPosition());
new Handler().postDelayed(() ->
mRecyclerView.getLayoutManager().startSmoothScroll(mSmoothScroller), 100);
}

当我同时单击两个具有 subview 的项目时,出现以下异常:

java.lang.IllegalArgumentException: Invalid target position
at android.support.v7.widget.RecyclerView$SmoothScroller.start(RecyclerView.java:11377)
at android.support.v7.widget.RecyclerView$LayoutManager.startSmoothScroll(RecyclerView.java:8090)
at com.sample.android.contact.ContactsAdapter$ViewHolder.lambda$onClick$0$ContactsAdapter$ViewHolder(ContactsAdapter.java:375)
at com.sample.android.contact.ContactsAdapter$ViewHolder$$Lambda$0.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

我不想删除 Handler.postDelayed,而是在寻找一种解决方案,使一次只能点击一个项目?

最佳答案

我通过将 setTargetPosition 移动到 Handler.PostDelayed 来解决问题:

new Handler().postDelayed(() -> {
mSmoothScroller.setTargetPosition(getAdapterPosition());
mRecyclerView.getLayoutManager().startSmoothScroll(mSmoothScroller);
}, 100);

关于android - 使用 SmoothScroller 时一次可点击单个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53035121/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com