81
86

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Fragmentを使った画面遷移

Posted at

Fragmentの使い方を調べてたら
Fragmentを利用して、画面遷移する世界一簡単なAndroidアプリを開発する
のブログが参考になった。
ただ、このレイアウト、ソースを書けばOKって感じの記載があったらもっとよかったので、それを記載。
※上記ブログだと、Main、Detailと記載がありますが、ここではFirst、Secondに変えています。

概要

  • 1ページ目をFirst、2ページをSecondとする。
  • Firstページで[Next]ボタンをクリックするとSecondページへ遷移するアプリを作ります。

AndroidManifest.xml

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.example.sample.FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.sample.SecondActivity"
            android:label="@string/app_name" >
        </activity>
        </application>
</manifest>

javaソース

com.example.sample 以下に全て配置

FirstActivity.java
package com.example.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class FirstActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        
    }

    void move() {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}
FirstFragment.java
package com.example.sample;

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

public class FirstFragment extends Fragment {

    private FirstActivity parent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_fragment, container, false);
        Button btnMove = (Button) view.findViewById(R.id.next);
        btnMove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                parent.move();
            }
        });
        return view;
    }
    
    @Override
    public void onAttach(Activity activity) {
        parent = (FirstActivity) activity;
        super.onAttach(activity);
    }
}
SecondActivity.java
package com.example.sample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class SecondActivity extends FragmentActivity {

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

}
SecondFragment.java
package com.example.sample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondFragment extends Fragment {

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

レイアウトXML

first.xml
<LinearLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <fragment android:id="@+id/fragment1" android:name="com.example.sample.FirstFragment" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
first_fragment.xml
<LinearLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="next"
        android:textColor="#ffffff"/>
</LinearLayout>
second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <fragment android:id="@+id/fragment2" android:name="com.example.sample.SecondFragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
second_fragment.xml
<LinearLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sencond" />

</LinearLayout>

以上、適当なプロジェクトを作って、上記ファイルに置き換えるだけでOK。

81
86
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81
86

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?