20160919
//activity_main.xmlに<Button と <fragment />を入れてFragmentを入れ替える
package com.example.kunihiro.myapplication;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ChangeFragment(View view){
android.app.Fragment fragment;
if(view == findViewById(R.id.button)){
fragment = new BlankFragment1();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment);
ft.commit(); }
if(view == findViewById(R.id.button2)){
fragment = new BlankFragment2();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment);
ft.commit();
}
}
}
//
package com.example.kunihiro.myapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
}
}
//
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment2 extends android.app.Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
}
//avtivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kunihiro.myapplication.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment1"
android:id="@+id/button"
android:onClick="ChangeFragment" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment2"
android:id="@+id/button2"
android:onClick="ChangeFragment" />
<!--defalt layout.FragmentOne android:name="layout.FragmentOne" -->
<fragment
android:name="com.example.kunihiro.myapplication.BlankFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_place"
tools:layout="@layout/fragment_blank_fragment1"></fragment>
</LinearLayout>
//fragment_blank_fragment1.xml
<FrameLayout
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="com.example.kunihiro.myapplication.BlankFragment1"
android:background="#ca3412">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="Fragment1" />
</FrameLayout>
//fragment_blank_fragment2.xml
<FrameLayout
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="com.example.kunihiro.myapplication.BlankFragment1"
android:background="#999999">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="Fragment2" />
</FrameLayout>
20160909
//activity_main.xmlに<fragment />を入れる
package com.example.kunihiro.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class BlankFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// 引数で渡ってきたinflaterを使ってレイアウトをinfrateする
//inflate: 拡張する,膨張させる 吹き込む
//メインのlayoutに別のレイアウトを動的に挿入することができる
final View layout = inflater.inflate(R.layout.fragment_blank, container);
// 必要に応じてUIにコールバックを設定する
layout.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getContext(), "Hello Fragment!!", Toast.LENGTH_SHORT).show();
}
});
// infrateされたViewをそのまま返す
return layout;
}
}
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kunihiro.myapplication.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:name="com.example.kunihiro.myapplication.BlankFragment"
android:id="@+id/fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
//fragment_blank.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="com.example.kunihiro.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_marginTop="62dp"
android:id="@+id/button" />
</RelativeLayout>