Android onClick自定义PageAdapter

时间:2015-11-04 08:29:57

标签: java android onclick onclicklistener

所以我创建了一个通用的PageAdapter,用于应用程序的各个部分,如下所示:

TcpClient

然后我在一个片段中调用此适配器,如下所示:

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;
    }

}

我的问题是,如何在片段中调用onClickListener。所以我的场景是,我们有一个图像轮播,一旦用户点击图像,它将打开该图像的大视图,因此需要一个onItemClickListener,但这只能在pagerAdapter中调用。 / p>

那么有没有办法在片段中调用onClickListener,或者在单击某个项目时从适配器通知片段?

1 个答案:

答案 0 :(得分:1)

这是对您的评论的回复。出于格式化和尺寸原因,我使用了答案。这是一个关于如何使用接口从适配器类中拆分片段的一般示例,这使得适配器可以在多个片段(甚至其他项目)中重用。

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.

   }
}

因此,在您的情况下,界面中的方法可能是onClick(int position);如果您需要多个方法,那么只需添加它们即可。