如何更改三个片段上的图像

时间:2014-06-05 15:04:38

标签: android static fragment

我有一个三片段项目。我想将每个片段上的图像从播放图像更改为暂停图像。我有一个叫做Global的公共课。从FragmentA我想调用Global.setCntrlImage()例程。此例程将更改三个片段上的三个图像。

如果FragmentA调用Global类。

                if (Global.player.isPlaying()) {    
                  Global.stopPlay();
                  image.setImageResource(R.drawable.play);
                  Global.setCntrlImage("play");
                  Global.playing = false;
                     }

这是我的全球课程。

public class Global  {

public static boolean playing = false;
public static MediaPlayer player = new MediaPlayer();

public static void stopPlay() {
    //Log.d(APP_TAG, "stopping playback..");
    //printResult("stop playing..");
    player.pause();
    player.reset();
    playing = false;
}

public static void startPlay(Context context, String fn) {
    // TODO Auto-generated method stub

        //Log.d(APP_TAG, "starting playback..");
        final String APP_TAG = "com.hascode.android.soundrecorder";

        if (player.isPlaying()){
            player.stop();
            player.reset();
            player.seekTo(0);
        }

        try {
            AssetFileDescriptor descriptor = context.getResources().getAssets().openFd(fn);
            player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            player.setLooping(true);
            player.prepare();
            player.start();
            playing = true;
        } catch (IllegalStateException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "illegal state .. player should be reset");
        } catch (IOException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "Could not write to sd card");
        }
        //System.out.println("playing = " + playing);
        //System.out.println("Song duration is " + player.getDuration());       
}


public static void setCntrlImage(String cntrl){
    //get image object from fragmentB, How?
    ImageView image = (ImageView) getView().findViewById(R.id.imageView1);
    if (cntrl == "play"){
        image.setImageResource(R.drawable.play);
    }else {
        image.setImageResource(R.drawable.pause);
    }       
}   

类型为Global的方法getView()未定义。如何更改三个片段A,B和C上的图像。

谢谢,

0 个答案:

没有答案