I need to have an ViewPager (similar to an horizontal gallery) inside an RecyclerView which display an list vertically. Each row of the RecyclerView will have an ViewPager which will allow to swipe between some images. The ViewPager will also support click events which will be propagated to the parent RecyclerView.
Currently, I have the following implementation:
List adapter:
@Override
public void onBindViewHolder(MyHolder holder, int position) {
super.onBindViewHolder(holder, position);
Item listItem = get(position);
...
GalleryAdapter adapter =
new GalleryAdapter(getActivity().getSupportFragmentManager(),
item.mediaGallery);
holder.imageGallery.setAdapter(adapter);
...
}
Gallery adapter:
public class GalleryAdapter extends FragmentStatePagerAdapter {
private final List<Item.Gallery> mItems;
@Bind(R.id.gallery_item)
ImageView galleryView;
public SearchResultsGalleryPagerAdapter(FragmentManager fm, @NonNull ArrayList<Item.Gallery> mediaGallery) {
super(fm);
mItems = mediaGallery;
}
@Override
public Fragment getItem(int position) {
GalleryFragment fragment = GalleryFragment.newInstance(mItems.get(position));
...
return fragment;
}
@Override
public int getCount() {
return null == mItems ? 0 : mItems.size();
}
@Override
public int getItemPosition(Object object) {
//return super.getItemPosition(object);
return PagerAdapter.POSITION_NONE;
}
}
Gallery fragment:
public class GalleryFragment extends Fragment {
private static final String GALLERY_ITEM_BUNDLE_KEY = "gallery_item_bundle_key";
@Bind(R.id.gallery_item)
ImageView mGalleryView;
private Item.Gallery mGalleryItem;
// Empty constructor, required as per Fragment docs
public GalleryFragment() {}
public static SearchResultsGalleryFragment newInstance(Item.Gallery galleryItem) {
GalleryFragment fragment = new GalleryFragment();
// Add the item in the bundle which will be set to the fragment
Bundle bundle = new Bundle();
bundle.putSerializable(GALLERY_ITEM_BUNDLE_KEY, galleryItem);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGalleryItem = (Item.Gallery) getArguments().getSerializable(GALLERY_ITEM_BUNDLE_KEY);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery_item, container, false);
ButterKnife.bind(this, view);
displayGalleryItem();
return view;
}
private void displayGalleryItem() {
if (null != mGalleryItem) {
Glide.with(getContext()) // Bind it with the context of the actual view used
.load(mGalleryItem.getImageUrl()) // Load the image
.centerCrop() // scale type
.placeholder(R.drawable.default_product_400_land) // temporary holder displayed while the image loads
.crossFade()
.into(mGalleryView);
}
}
}
The issue I'm having is that the fragments of the ViewPager aren't created and displayed correctly. Sometimes they appear after an manual scroll (but not always), in most of the cases they don't appear at all.
Does anyone have an idea on what I've implemented wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…