0

在我的项目中,我将片段用于交换屏幕,当新屏幕出现时,在后台自动播放音频。如何实现这一点请帮助...

public class Main extends FragmentActivity{

    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.view);

        this.initialisePaging();
    }

    private void initialisePaging() 
    {

        List<Fragment> fragments = new Vector<Fragment>();

        fragments.add(Fragment.instantiate(this, Tab1Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment2.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment3.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment4.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment5.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment6.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);


        ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }
}

这是pagerview....

package com.example.swape;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }


    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

这是显示带有文本的图片并在背景中播放音频的片段屏幕....

package com.example.swape;



import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


//import com.andy.R;


public class Tab1Fragment1 extends Fragment {

    String str = "DEATH WARENT";    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab_frag1_layout1,
                container, false);
        TextView tv = (TextView)view.findViewById(R.id.tv1); 
    //  Button playsong = (Button)view.findViewById(R.id.play);
        tv.setText("Android introduced fragments in Android 3.0 (API level 11), " +
                "primarily to support more dynamic and flexible UI designs on large screens, such as tablets. " +
                "Because a tablet's screen is much larger " +
                "than that of a handset, there's more room to combine and interchange UI components.");

        //myStartPreview();
        // myStopPreview();
//      playsong.setOnClickListener(new View.OnClickListener() {
//          
//          public void onClick(View arg0) {
//              // TODO Auto-generated method stub
//              
//              
//              
//              Log.e("","Start==hh"+str);
//          }
//
//          
//      });

        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        Log.e("","StartAttach=="+str);

    }
//  @Override
     public void myStartPreview( View view) {

         Log.e("","StartPreview=="+str);

     }

     public void myStopPreview(View view){
         Log.e("","StopPreview=="+str); 
     }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Log.e("","StartCreated=="+str);

    }

    @Override
    public void onStart(){
        super.onStart();
        Log.e("","Start=="+str);
    }
    @Override
    public void onResume(){
        super.onResume();
        Log.e("","Resume=="+str);

    }

    @Override
    public void onPause(){
        super.onPause();
        Log.e("","Pause=="+str);

    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.e("","Destroy=="+str);

    }
}

这是我使用的概念的示例代码...

4

1 回答 1

0

PagerAdapter 有一个类似的 onPageChangeListener 。使用它来收听页面更改而不是播放音频。

于 2013-03-06T12:45:17.580 回答