简体   繁体   中英

Implement onClickListener on fragment inside FragmentStatePagerAdapter

Good day,

I have ViewPager , and this is adapter:

public abstract class GalleryImageAdapter extends FragmentStatePagerAdapter {

    private List<? extends UrlItem> images = new ArrayList<>();

    public GalleryImageAdapter(FragmentManager fm, List<? extends UrlItem> images) {
        super(fm);
        this.images = images;
    }

    @Override
    public Fragment getItem(int position) {
        return giveFragment(images, position);
    }

    public abstract Fragment giveFragment(List<? extends UrlItem> images, int position);

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(collection, position);
        return fragment;
    }
}

I have a ViewPager , which represents gallery of images. I would like to handle click on current selected child (fragment) in ViewPager .

I found something about instantiateItem , but nothing about how to handle clicks in FragmentStatePagerAdapter . I got the fragment in this method but .setOnClickListener is not found

If it's not needed for your image gallery items to have their own lifecycles (and I would imagine it's not), don't use Fragments and FragmentStatePagerAdapter. Use a RecyclerView with the appropriate adapter, as explained, for example, here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM