Viewpager中的按钮不会更新

时间:2014-03-26 21:15:15

标签: android button android-viewpager

嗨,我对android很新。我想要做的是,我的viewPager中有图片,顶部有一个按钮。如果按一个按钮,将播放声音。但是当我滑到下一页时,如果我再次按下按钮,它应该根据新的图片播放新的声音。以下是我的代码。但它播放了之前图片中的旧声音。它不会使用新的位置值进行更新。请帮我解决问题。

OnPageChangeListener player;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    //setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.view_pager);
    final ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
    viewpager.setOnPageChangeListener(player);
    //

    //
    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapterchap1 imageAdapter = new ImageAdapterchap1(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        images.add(imageView);


    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);

    viewpager.setAdapter(pageradapter);

    // Show images following the position
    viewpager.setCurrentItem(position);

   final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.car_alarm);
    final MediaPlayer cpButtonClick = MediaPlayer.create(this, R.raw.car_ignition);
    final MediaPlayer fpButtonClick = MediaPlayer.create(this, R.raw.helicopter);
    final MediaPlayer ffButtonClick = MediaPlayer.create(this, R.raw.motorcycle);


    Button info=(Button) findViewById(R.id.audio);
    info.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        switch (viewpager.getCurrentItem()) {
        case 0:
            mpButtonClick.start();

            // do something
            break;
        case 1:
            cpButtonClick.start();
            // do something
            break;
        case 2:
            fpButtonClick.start();
            // do something
            break;
        case 3:
            ffButtonClick.start();
            // do something
            break;
        case 4:
            // do something
            break;
        }
    }

    });

    OnPageChangeListener player = new OnPageChangeListener()
    {

        @Override
        public void onPageSelected(int arg1)
        {

        ffButtonClick.stop();
        mpButtonClick.stop();
        cpButtonClick.stop();
        fpButtonClick.stop();
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
        }

        @Override
        public void onPageScrollStateChanged(int arg0)
        {
        }
    };


    }


    } 



public void destroyItem( View pager, int position, Object view )
{
    ((ViewPager)pager).removeView( (View)view );
}

}

1 个答案:

答案 0 :(得分:0)

您正在使用的position变量无法在任何地方进行更新。如果您想知道ViewPager中的当前项目位置,您应该使用:

viewpager.getCurrentItem();

或使用positionOnPageChangeListener方法更新viewpager.setOnPageChangeListener(listener)变量。

修改

viewpager.getCurrentItem()应该用作开关的参数:

switch(viewpager.getCurrentItem()) {
    ...
}