2012-09-11 268 views
1

我想在選擇頁面時在viewpager上顯示消息並播放聲音。我看到下一條消息。如果選擇了頁面(圖片1 - 聲音2,圖片2 - 聲音3 ...) 我想在選擇頁面時播放聲音(圖片1 - 聲音1,圖片2 - 聲音2,....)問題在哪裏? 謝謝,Viewpager無法正常工作

Viewpager;

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

Mypageradapter;

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

} 

回答

0

請試穿ViewPagersetOnPageChangeListener設置和onPageSelected,你將能夠獲得形而下頁。