我正在创建一个应用程序,其中我有多个图像,声音和文本。我想在屏幕上显示单个图像,为此我使用视图寻呼机并在页面上实现更改监听器界面以跟踪页面。但是在我的情况下,图像是我想要的滚动,但我想播放声音,因为每个图像的每个声音是不同的,在我的情况下,前2个声音同时播放,正在设置的文本在数组中,它是第二个指数位置。我无法解决问题。任何建议都将受到高度赞赏。 这是我的代码,
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setOnPageChangeListener(adapter);
viewPager.setAdapter(adapter);
私有类ImagePagerAdapter扩展PagerAdapter实现了ViewPager.OnPageChangeListener {
Context context;
MediaPlayer mp;
private int[] mImages = new int[]{R.drawable.airplane,R.drawable.ambulance,R.drawable.apple,
R.drawable.ball_img,R.drawable.bear_img,R.drawable.bird_img,
R.drawable.cake_img,R.drawable.cat,R.drawable.car,R.drawable.clock,
R.drawable.drum_img,R.drawable.dinasour,R.drawable.dog,R.drawable.dolphine,R.drawable.duck,
R.drawable.eagle,R.drawable.elephant,
R.drawable.fish,R.drawable.frog,
R.drawable.horse,
R.drawable.insect,
R.drawable.jagular,
R.drawable.kangroo ,
R.drawable.lion,
R.drawable.monkey,
R.drawable.nose,
R.drawable.owl ,
R.drawable.peacock ,
R.drawable.quill,
R.drawable.rabit ,
R.drawable.star ,
R.drawable.tiger,
R.drawable.umbrella ,
R.drawable.vulture,
R.drawable.wolf ,
R.drawable.xylophone ,
R.drawable.yoyo ,
R.drawable.zebra
};
private int [] soundIds = {R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance};
String description [] = {"Airplane","Ambulance","Apple","Ball","Bear","Bird","Cake","Cat","Car","Clock"
,"Drum","Dinasour","Dog","Dolphine","Duck","Eagle","Elephant","Fish","Frog","Horse","Insect"
,"Jagular","Kangroo","Lion","Monkey","Nose","Owl","Peacock","Quill","Rabbit","Star","Tiger"
,"Umbrella","Vulture","Wolf","Xylophone","Yoyo","Zebra"};
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container,int position) {
context = ZoomedLetters.this;
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mp = MediaPlayer.create(context,soundIds[position]);
mp.start();
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
int padding = context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;*/
}
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}