的ImageAdapter
類如何爲viewpager中的每個幻燈片設置onClickListeners?
public class ImageAdapter extends PagerAdapter implements AdapterView.OnItemClickListener {
Context context;
private int[] GalImages = new int[] {//array storing all images used in the 'slideshow' in the main menu
R.drawable.savedpageex,
R.drawable.ic_cdi_icon,
R.drawable.savedpageex,
R.drawable.ic_cdi_icon
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
// int padding = context.getResources().getDimensionPixelSize(R.dimen.abc_panel_menu_list_width);
imageView.setPadding(1, 1, 1, 1);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int clickedImage = GalImages[position];
}
}
在MainActivity
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
ImageAdapter adapter = new ImageAdapter(this);
wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(wrappedAdapter);
viewPager.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
眼下,圖片顯示了,他們循環,我需要幫助,在設定的時間間隔獲取圖片「幻燈片」,但主要問題是在每個「頁面」上設置ClickListeners。任何幫助/建議?
您是否嘗試覆蓋''public void onItemClick(AdapterView > parent,View view,int position,long id)'方法? –
它尚未被覆蓋? – Michael