Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

java - An instance of OnFlingListener already set in Recyclerview

I'm using a RecyclerView in my Android app. I've many cardviews in my RecyclerView, such that only 1 cardview is displayed to the user at a time. The user has to swipe to see the next card.

I faced an issue that when user makes a swipe, the recyclerview gets scrolled to the end. Instead, what I need is when the user swipes, display the next card to the user as mentioned in this post.

How to make swipe on horizontal recyclerview bring only the next item into view - Android

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

I tried the answer mentioned in the above link. But I'm getting an exception when the user makes a swipe.

Please help me resolve it.

Error

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)
question from:https://stackoverflow.com/questions/44043501/an-instance-of-onflinglistener-already-set-in-recyclerview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed to sovle this with adding this line

recyclerView.setOnFlingListener(null); 

before

snapHelper.attachToRecyclerView(recyclerView);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...