1

我想在选择页面时在 viewpager 上显示消息和播放声音。我正在看到下一条消息。如果选择了页面(图片 1 - 声音 2,图片 2 - 声音 3...),我想在选择页面时播放声音(图片 1-声音 1,图片 2-声音 2,...)问题出在哪里?谢谢,

浏览器;

package com.example.messages;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class Pictures extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picturespage);
 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(0); 
}
};

我的页面适配器;

package com.example.messages;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class MyPagerAdapter extends  PagerAdapter{

SoundPool snd;
int sound1,sound2,sound3;
boolean loaded = false;

public int getCount() {
    return 6;
}

public Object instantiateItem(View collection, int position) {
    View view=null;
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Set volume rocker mode to media volume
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    snd = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
    snd.setOnLoadCompleteListener(new OnLoadCompleteListener() {
      public void onLoadComplete(SoundPool soundPool, int sampleId,
          int status) {
        loaded = true;
      }
    });
    sound1 = snd.load(collection.getContext(), R.raw.sound1, 1);
    sound2 = snd.load(collection.getContext(), R.raw.sound2, 1);
    sound3 = snd.load(collection.getContext(), R.raw.sound3, 1);

    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.picture1;//
        view = inflater.inflate(resId, null);
        if (loaded) {
            snd.play(sound1, 1, 1, 1, 0, 1f);
            Toast.makeText(collection.getContext(), "Sound1", Toast.LENGTH_SHORT).show();
          }

        break;
    case 1:
        resId = R.layout.picture2; //    
        view = inflater.inflate(resId, null);
        if (loaded) {
            snd.play(sound2, 1, 1, 1, 0, 1f);
            Toast.makeText(collection.getContext(), "Sound2", Toast.LENGTH_SHORT).show();
          }
        break;
    case 2:
        resId = R.layout.picture3;//
        view = inflater.inflate(resId, null);
        if (loaded) {
            snd.play(sound3, 1, 1, 1, 0, 1f);
            Toast.makeText(collection.getContext(), "Sound3", Toast.LENGTH_SHORT).show();
          }
        break;
    case 3:
        resId = R.layout.picture4;//
        view = inflater.inflate(resId, null);
        Toast.makeText(collection.getContext(), "Test4", Toast.LENGTH_SHORT).show();
        break;
    case 4:
        resId = R.layout.picture5;//
        view = inflater.inflate(resId, null);
        Toast.makeText(collection.getContext(), "Test5", Toast.LENGTH_SHORT).show();

        break;
    case 5:
        resId = R.layout.picture6;//
        view = inflater.inflate(resId, null);
        Toast.makeText(collection.getContext(), "Test6", Toast.LENGTH_SHORT).show();
        break;
    }


    ((ViewPager) collection).addView(view, 0);

    return view;
}

@SuppressWarnings("unused")
private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

private void setVolumeControlStream(int streamMusic) {
    // TODO Auto-generated method stub

}


@SuppressWarnings("unused")
private Context getBaseContext() {
    // TODO Auto-generated method stub
    return null;
}


@SuppressWarnings("unused")
private PagerAdapter findViewById(int myfivepanelpager) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
    return null;
}

public static Integer getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

}
4

1 回答 1

0

请尝试设置一个ViewPager setOnPageChangeListener,然后onPageSelected您将能够获得具体页面。

于 2012-09-11T07:05:42.467 回答