AS中級 フラグメント編①問題&解説~Fragmentで1画面に2つの画面を入れる~
Fragmentとは何か?
FragmentActivityを略したものであるようである。Activityを継承してます。
やっと完成しました。
MainActivity.java
package com.example.fragmentdemo3;
import static android.graphics.Color.RED;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TopFragment topFragment = TopFragment.newInstance(Color.RED);
BottomFragment bottomFragment = BottomFragment.newInstance(Color.BLUE);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction frgTra = fragmentManager.beginTransaction();
frgTra.add(R.id.container1,topFragment);
frgTra.add(R.id.container2,bottomFragment);
frgTra.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/container1"/>
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/container2"/>
</LinearLayout>
TopFragment.java
package com.example.fragmentdemo3;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class TopFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_top, container, false);
}
public static TopFragment newInstance(int data){
Bundle args = new Bundle();
args.putInt("data1",data);
TopFragment topFragment = new TopFragment();
topFragment.setArguments(args);
return topFragment;
}
}
BottomFragment.java
package com.example.fragmentdemo3;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class BottomFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_bottom, container, false);
}
public static BottomFragment newInstance(int data){
Bundle args = new Bundle();
args.putInt("data2",data);
BottomFragment bottomFragment = new BottomFragment();
bottomFragment.setArguments(args);
return bottomFragment;
}
}
fragment_top.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FF0000"
tools:context=".TopFragment">
<TextView
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#063"/>
</LinearLayout>
fragment_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF00FF"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".BottomFragment">
<TextView
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#063"/>
</LinearLayout>