ImageAdapter
班级
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();
}
});
现在,图片显示并且循环播放,我确实需要帮助让图片“滑动”。在设定的时间间隔,但主要问题是为每个页面设置onClickListeners&#39;。有什么帮助/建议吗?
答案 0 :(得分:0)
您可以在onClickListener中添加一个switch语句,如下所示
switch(viewPager.getCurrentItem()){
case 0:
//stuff for first page
break;
case 1:
//stuff for second page
break;
}
......等等。
或向viewpager添加状态以描述它正在显示的“页面” 并使用该信息在onClickListener中执行不同的操作