James King
James King

Reputation: 2445

Android onClick custom PageAdapter

So I have created a generic PageAdapter to be used in various parts on the app, which looks like this:

public class ImagePagerAdapter extends PagerAdapter {

    private final LayoutInflater layoutInflater;
    private final Picasso picasso;
    private final int layoutResId;

    private final List<AssociatedMedia> images;

    public ImagePagerAdapter(Context context, int layoutResId) {
        layoutInflater = LayoutInflater.from(context);
        picasso = Injector.getInstance().getPicasso();
        this.layoutResId = layoutResId;
        this.images = new ArrayList<>();
    }

    public void setMedia(List<AssociatedMedia> media) {
        images.clear();
        for (AssociatedMedia productMedia : media) {
            if (productMedia.type == AssociatedMediaType.IMAGE) {
                images.add(productMedia);
            }
            else {
                // non-images all at the end
                break;
            }
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        AssociatedMedia image = images.get(position);

        ImageView imageView = (ImageView) layoutInflater.inflate(layoutResId, container, false);
        container.addView(imageView);

        picasso.load(Uri.parse(image.urls[0])).into(imageView);

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ImageView imageView = (ImageView) object;
        container.removeView(imageView);

        picasso.cancelRequest(imageView);
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}

I then call this adapter in a fragment, like this:

ImagePagerAdapter productImageAdapter = new ImagePagerAdapter(getActivity(), R.layout.photo_container_small);
            productImageAdapter.setMedia(medias);
            productImage.setAdapter(productImageAdapter);

My question is, how can I invoke a onClickListener in the fragment. So my scenario is that, we have a carousel of images, and once the user click on an image, it will open a large view on that image, so sort of need an onItemClickListener, but this can only be invoked in the pagerAdapter.

So is there a way to either call a onClickListener in the fragment, or notify the fragment from the adapter when an item has been clicked?

Upvotes: 0

Views: 51

Answers (1)

Hermann Klecker
Hermann Klecker

Reputation: 14068

This is a response to your comment. For formating and size reasons I use an answer for it. It is a general example on how to use an interface to de-couple a fragment from an adapter class which makes the adapter re-usable in several fragments (and even other projects).

public class MyAdapter {

   MyAdapterListener listener;

   private MyAdapter() {}

   public MyAdapter(MyAdapterListener listeningActivityOrFragment) {

      listener = listeningActivityOrFragment;

   }
}



public interface MyAdapterListener {

   void somethingTheFragmentNeedsToKnow(Object someData);

}


public class SomeFragment extends Fragment implements  MyAdapterListener {

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.my_view, container, false);

      // Do everyhting here to init your view.

      // Create an Adapter and bind it to this fragment
      MyAdapter myAdapter = new MyAdapter(this);

      return view;

   }

// Implement the listener interface

   @Override
   public void somethingTheFragmentNeedsToKnow(Object someData) {

      // Get the data and process it.

   }
}

So in your case the method within the interface may well be onClick(int position); If you need more than one method, then just add them.

Upvotes: 1

Related Questions