Viewpager 无法正常工作

Posted

技术标签:

【中文标题】Viewpager 无法正常工作【英文标题】:Viewpager not working correctly 【发布时间】:2012-09-11 06:57:23 【问题描述】:

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

浏览器;

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;



【问题讨论】:

【参考方案1】:

请尝试在ViewPager setOnPageChangeListener 上设置,在onPageSelected 中您将能够获得具体页面。

【讨论】:

以上是关于Viewpager 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

ViewPager pageSelected无法正常工作

Android:WebView Scrolling 在 Vertical Directional Viewpager 中无法正常工作

如何将 PreferenceFragment 与 ViewPager 一起使用?

ViewPager 上的菜单按钮敬酒位置错误

ViewPager 中的 Android Master/Detail 流程

Viewpager内滚动视图