Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed to to reset carousel position when CarouselItem is displayed again #309

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public GroupieViewHolder<ItemCarouselBinding> createViewHolder(@NonNull View ite
RecyclerView recyclerView = viewHolder.binding.recyclerView;
recyclerView.addItemDecoration(carouselDecoration);
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(adapter);
return viewHolder;
}

@Override public void bind(@NonNull ItemCarouselBinding viewBinding, int position) {
viewBinding.recyclerView.setAdapter(adapter);
}

@Override public int getLayout() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xwray.groupie.example.item

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.xwray.groupie.GroupAdapter
Expand All @@ -18,11 +19,17 @@ class CarouselItem(private val carouselDecoration: RecyclerView.ItemDecoration,
return R.layout.item_carousel
}

override fun createViewHolder(itemView: View): GroupieViewHolder {
return super.createViewHolder(itemView).apply {
recyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = carouselAdapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause an issue if you have more than one CarouselItem in the adapter and you rebind the item. It means both will share the adapter of the first one instead of the 2nd one having a new adapter set because createViewHolder will only have been called once.

}
}
}

override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.recyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = carouselAdapter

// We don't know if the layout we're passed has been bound before so
// we need to ensure we don't register the item decoration multiple times,
// by trying to remove it first. (Nothing happens if it's not registered.)
Expand Down