jama5262 / CarouselView

An android carousel library for RecyclerView
https://jitpack.io/#jama5262/CarouselView
MIT License
236 stars 47 forks source link

Calling show() multiple times results in an IllegalStateException #20

Closed DarioAhdoot closed 3 years ago

DarioAhdoot commented 4 years ago

Describe the bug Calling show() multiple times results in an IllegalStateException being thrown by SnapHelper::setupCallbacks, throw new IllegalStateException("An instance of OnFlingListener already set.");

To Reproduce Steps to reproduce the behavior:

  1. Load an initial data source, call show(). Works as expected
  2. Load a subsequent data source and call show() again
  3. See crash

Expected behavior Should support calling show() multiple times to repopulate the carousel

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

Maybe I'm doing something wrong, but, I don't see a way to get the carousel to reload data other than calling show(). Here is my code:

        binding.carouselView.apply {
          size = assets.size
          resource = R.layout.view_asset_item
          autoPlay = false
          enableSnapping(true)
          hideIndicator(true)
          indicatorAnimationType = IndicatorAnimationType.NONE
          carouselOffset = OffsetType.CENTER
          setCarouselViewListener { view, position ->
            val assetItemView = view as AssetItemView
            assetItemView.setAsset(assets[position])
          }
          show()
        }
DarioAhdoot commented 4 years ago

Also tried this instead of calling show multiple times, but I was not able to get the data to reload:

        binding.carouselView.size = assets.size
        requireView().findViewById<RecyclerView>(R.id.carouselRecyclerView).adapter!!.notifyDataSetChanged()
rohanisme commented 4 years ago

Same issues here as well can some one help me solve this

DarioAhdoot commented 4 years ago

@rohanisme I had to just fork the repo and make a bunch of changes to accommodate that and other needs. This library seems to be designed for static data that doesn't change

rohanisme commented 4 years ago

Can u share me the changed repo so i can use the same

DarioAhdoot commented 4 years ago

@rohanisme I can't unfortunately, but it wasn't hard

ashharjamil commented 4 years ago

before apply set onFlingListener null

binding.carouselView.findViewById(R.id.carouselRecyclerView).onFlingListener = null

derysudrajat commented 3 years ago

I've got the same problem, I just made it static, I am using live data to load the update news data when it was added to the bookmark and really not affected in the carousel, so I just create an instance of my data, and check it, if it was empty add and load data carousel

private var carouselData = listOf<ArticlesItem?>()

in observer

model.getNews().observe(viewLifecycleOwner, {
     if (carouselData.isEmpty()) {
         carouselData = it.articles!!
         setDataCarousel(carouselData)
     }    
})

I've tried to remove all data before apply but it doesn't work too

krishh95 commented 3 years ago

before apply set onFlingListener null

binding.carouselView.findViewById(R.id.carouselRecyclerView).onFlingListener = null

this was better solution to resolve issue

shubhamkodes commented 3 years ago

val v: RecyclerView = binding.videoSlider!!.findViewById(R.id.carouselRecyclerView) v.onFlingListener = null

it's is working perfectly

jama5262 commented 3 years ago

Solved in #26