Android 如何将活动转换为片段?

Android 如何将活动转换为片段?,android,Android,我想将游戏活动转换为游戏片段。有人能帮我吗? 这是游戏活动: public class Game extends BaseGameActivity implements IOnSceneTouchListener, Observer{ private static int CAMERA_HEIGHT = 480; private static int CAMERA_WIDTH = 800; private Camera mCamera; private Tiled

我想将游戏活动转换为游戏片段。有人能帮我吗?
这是游戏活动:

public class Game extends BaseGameActivity implements IOnSceneTouchListener, Observer{
    private static int CAMERA_HEIGHT = 480;
    private static int CAMERA_WIDTH = 800;
    private Camera mCamera;
    private TiledTextureRegion ballTextureRegion;
    private Texture paddleTexture;
    private Texture ballTexture;
    private Paddle paddle;
    private GameHolder gameHolder;
    private Object mCurViewMode;
    public Engine onLoadEngine() 
    {
        final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
        CAMERA_WIDTH = defaultDisplay.getWidth();
        CAMERA_HEIGHT = defaultDisplay.getHeight();
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources()
    {
        gameHolder = GameHolder.getInstance();
        gameHolder.addObserver(this);
        gameHolder.setGameActivity(this);

        this.ballTexture = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.paddleTexture = new Texture(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

        TextureRegionFactory.setAssetBasePath("gfx/");

        this.ballTextureRegion = TextureRegionFactory.createTiledFromAsset(this.ballTexture, this, "ball.png", 0, 0, 1, 1);

        this.mEngine.getTextureManager().loadTexture(this.paddleTexture);
        this.mEngine.getTextureManager().loadTexture(this.ballTexture);
    }
    @Override
    public Scene onLoadScene() 
    {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene(1);
        scene.setBackground(new ColorBackground(0f, 0f, 0f));
        scene.setOnSceneTouchListener(this);

        final Ball ball = new Ball(CAMERA_WIDTH/2, CAMERA_HEIGHT-30-16, this.ballTextureRegion, mEngine);
        ball.setVelocity(100.0f, 100.0f);

        scene.getTopLayer().addEntity(ball);

        paddle = new Paddle(CAMERA_WIDTH/2, CAMERA_HEIGHT-50, CAMERA_HEIGHT/8, CAMERA_WIDTH/34);

        final Brick[][] bricks = new Brick[5][5];

        for (int i = 0; i < bricks.length; i++) {
            for (int j = 0; j < bricks[0].length; j++) {
                bricks[i][j]= new Brick(10+j*CAMERA_WIDTH/5, 10+i*CAMERA_HEIGHT/15 , CAMERA_HEIGHT/16, CAMERA_WIDTH/32);
                scene.getTopLayer().addEntity(bricks[i][j]);
            }
        }

        scene.getTopLayer().addEntity(paddle);
        scene.getTopLayer().addEntity(ball);
        scene.registerTouchArea(paddle);

        /* The actual collision-checking. */
        scene.registerUpdateHandler(new IUpdateHandler() {
            public void reset() { }

            public void onUpdate(final float pSecondsElapsed) {
                if(ball.collidesWith(paddle)) {
                    ball.bounceWithRectangle(paddle);
                }
                else if (ball.getY() >= Game.getCAMERA_HEIGHT() - 30) {
                    scene.setBackground(new ColorBackground(255f, 0f, 0f));
                    Log.w("Game.java","Game Over");
                }
                else {
                    for (int i = 0; i < bricks.length; i++) {
                        for (int j = 0; j < bricks[0].length; j++) {
                            scene.setBackground(new ColorBackground(0f, 0f, 0f));
                            if(ball.collidesWith(bricks[i][j])) {
                                bricks[i][j].setPosition(CAMERA_HEIGHT+20, CAMERA_WIDTH+20);
                                scene.getTopLayer().removeEntity(bricks[i][j]);
                                ball.bounceWithRectangle(bricks[i][j]);

                            }
                        }
                    }
                }
            }
        });

        return scene;
    }

    public static int getCAMERA_HEIGHT() {
        return CAMERA_HEIGHT;
    }
    public static int getCAMERA_WIDTH() {
        return CAMERA_WIDTH;
    }

    @Override
    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        paddle.setPosition(pSceneTouchEvent.getX() - paddle.getWidth() / 2, Game.getCAMERA_HEIGHT()-30);
        return true;
    }

    protected void onPause() {
        super.onPause();
        //commented on error
        //gameHolder.setGameState(gameHolder.getPausedGameState());
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub

    }
}
公共类游戏扩展BaseGameActivity实现了OnSceneTouchListener、Observer{
专用静态int摄像机的高度=480;
专用静态int摄像机_宽度=800;
私人摄像机麦卡梅拉;
私人平铺纺织区;
私有纹理;
私人纹理;
私人桨;
私人玩家;
私有对象mCurViewMode;
公共引擎onLoadEngine()
{
最终显示defaultDisplay=getWindow().getWindowManager().getDefaultDisplay();
CAMERA_WIDTH=defaultDisplay.getWidth();
CAMERA_HEIGHT=defaultDisplay.getHeight();
this.mCamera=新摄像头(0,0,摄像头宽度,摄像头高度);
返回新引擎(新引擎选项(true、ScreenOrientation.Grait、新比率解决方案策略(CAMERA_WIDTH、CAMERA_HEIGHT)、this.mCamera));
}
@凌驾
公共void onLoadResources()
{
gameHolder=gameHolder.getInstance();
gameHolder.addObserver(这个);
gameHolder.setGameActivity(本);
this.ballTexture=新纹理(64,64,TextureOptions.BILINEAR\u PREMULTIPLYALPHA);
this.paileTexture=新纹理(1024,1024,TextureOptions.BILINEAR\u PREMULTIPLYALPHA);
TextureRegionFactory.setAssetBasePath(“gfx/”);
this.ballTextureRegion=TextureRegionFactory.createTiledFromAsset(this.ballTexture,this,“ball.png”,0,0,1,1);
this.mEngine.getTextureManager().loadTexture(this.PadderTexture);
this.mEngine.getTextureManager().loadTexture(this.ballTexture);
}
@凌驾
公共场景onLoadScene()
{
this.mEngine.registerUpdateHandler(新的FPSLogger());
最终场景=新场景(1);
场景.背景(新的彩色背景(0f,0f,0f));
scene.setOnSceneTouchListener(此);
最后一个球=新球(摄像头宽度/2,摄像头高度-30-16,this.ballTextureRegion,mEngine);
设定速度(100.0f,100.0f);
scene.getTopLayer().addEntity(ball);
挡板=新挡板(摄像头宽度/2,摄像头高度-50,摄像头高度/8,摄像头宽度/34);
最终砖[]砖=新砖[5][5];
对于(int i=0;i=Game.getCAMERA_HEIGHT()-30){
场景.背景(新彩色背景(255f,0f,0f));
Log.w(“Game.java”,“游戏结束”);
}
否则{
对于(int i=0;i
您可以将活动更改为如下片段:

public class FragmentClass extends Fragment  
{  
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {            
        view = (View) inflater.inflate(R.layout.layout_history, container,false);

        //Do all things which you want to implement //enter code here
        return view;
    }
}
您需要扩展片段并重写其
onCreateView(LayoutInflater、ViewGroup、Bundle)
方法。 使用上面的布局和返回视图对其进行充气

您还可以使用其视图和传递上下文创建此片段的成员,方法是:

getActivity();
如果您想使用
findViewById(int)
,那么您可以像这样使用它:

getView().findViewById(int);
如果您想了解更多信息,请访问: &

您可以像下面这样创建自定义片段。所有这些之后,您可以使用fragment类中的getActivity()来使用您的方法

关于方法的错误,则必须使用当前片段类的上下文。
示例如果要使用TextView,则:

TextView tv = (TextView) context.findViewById(R.id.textView);
我正在展示如何使用BaseActivity生成片段的示例:

public class MainActivity extends BaseActivity 
{
    private FragmentClass fragmentClass;
    private ViewPager mViewPager;

    private SlidingTabLayout mSlidingTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //Do all stub
        populateView();
    }

    private void populateView()
    {
        mViewPager = (ViewPager) findViewById(R.id.pager1);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setDividerColors(color);
        mSlidingTabLayout.setCustomTabView(R.layout._custom_tab, 0);
        mSlidingTabLayout.setViewPager(mViewPager);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter
    {
        public ViewPagerAdapter(FragmentManager fm) 
        {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int pos) 
        {
            if(pos==0)
            {
                fragmentClass = new FragmentClass();
                return fragmentClass;
            }
            return null;                
        }  

        @Override
        public int getCount() 
        {
            // TODO Auto-generated method stub
            return 1;
        }
    }
}
从developer.android添加两个类:

  • 滑动选项卡布局
  • 滑动凸耳条

  • 您可以将活动更改为如下片段:

    public class FragmentClass extends Fragment  
    {  
        private View view;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {            
            view = (View) inflater.inflate(R.layout.layout_history, container,false);
    
            //Do all things which you want to implement //enter code here
            return view;
        }
    }
    
    您需要扩展片段并重写其
    onCreateView(LayoutInflater、ViewGroup、Bundle)
    方法。 使用上面的布局和返回视图对其进行充气

    您还可以创建memb