我在Android应用中使用了RecyclerView。我的RecyclerView中有很多卡片视图,因此一次只能向用户显示1张卡片视图。用户必须滑动才能看到下一张卡片。
我遇到了一个问题,当用户进行滑动时,recyclerview会滚动到最后。相反,我需要的是当用户滑动时,如本文所述,向用户显示下一张卡片。
How to make swipe on horizontal recyclerview bring only the next item into view - Android
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
我尝试了上面链接中提到的答案。但是当用户进行滑动时,我会收到异常。
请帮我解决。
错误
java.lang.IllegalStateException: An instance of OnFlingListener already set.
at android.support.v7.widget.SnapHelper.setupCallbacks(SnapHelper.java:114)
at android.support.v7.widget.SnapHelper.attachToRecyclerView(SnapHelper.java:102)
at com.abc.ui.trm.TrCard.setupCardView(TrCard.java:62)
at com.abc.ui.trm.TrCard.setupCardView(TrCard.java:29)
at com.abc.ui.core.card.BaseCardView.processCardView(BaseCardView.java:134)
at com.abc.ui.card.CardRecyclerAdapter.onViewAttachedToWindow(CardRecyclerAdapter.java:398)
at android.support.v7.widget.RecyclerView.dispatchChildAttached(RecyclerView.java:6758)
at android.support.v7.widget.RecyclerView$5.addView(RecyclerView.java:696)
at android.support.v7.widget.ChildHelper.addView(ChildHelper.java:107)
at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:7697)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7655)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7643)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1539)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at com.abc.ui.core.refresh.LegacySwipeRefreshLayout.onLayout(LegacySwipeRefreshLayout.java:337)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:822)
at android.view.View.layout(View.java:18799)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
答案 0 :(得分:40)
我设法通过添加这一行来解决这个问题
recyclerView.setOnFlingListener(null);
之前
snapHelper.attachToRecyclerView(recyclerView);
答案 1 :(得分:5)
您可以先在RecyclerView上检查OnFlingListener,然后再添加
if (recycler.getOnFlingListener() == null)
snapHelper.attachToRecyclerView(recycler);
答案 2 :(得分:4)
您所要做的就是放置
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
在 onCreateViewHolder 中,而不是 onBindViewHolder
答案 3 :(得分:4)
发生这种情况是因为您以某种方式两次设置了SnapPagerHelper
。请注意,如果您在另一个RecyclerView
中有一个RecyclerView
,则此情况仅应发生在彼此的构造函数或Kotlin的init {}
块中(每项一次)。
我的情况与您的情况非常相似,因为我的内部有一个大的RecyclerView
和一个小的RecyclerView
。
将导致异常的代码:
fun bind(value: ArrayList<Item>) {
listAdapter = ItemCellDelegate(value)
val manager = LinearLayoutManager(itemView.context)
manager.orientation = LinearLayoutManager.HORIZONTAL
horizontalRecyclerView?.apply {
layoutManager = manager
adapter = listAdapter
setHasFixedSize(true)
}
val snapHelper: SnapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(horizontalRecyclerView)
}
使用init {}
块纠正代码:
init {
val snapHelper: SnapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(horizontalRecyclerView)
}
答案 4 :(得分:1)
放置以下行:
mRecyclerView.setOnFlingListener(null);
此行上方:
new LinearSnapHelper().attachToRecyclerView(mRecyclerView)
答案 5 :(得分:0)
像这样:
override fun onAttachedToWindow(view: RecyclerView) {
super.onAttachedToWindow(view)
view.onFlingListener = null;
StartSnapHelper().attachToRecyclerView(view)
}