Android

Fragment

 

Fragment 간 데이터 전송 (setArguments() 와 getArguments() 이용)

 

MainActivity.java (onCreate)

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.frameLayout, fragment1);
transaction.commit(); // 저장

 

activity_main.xml

<ConstraintLayout>
<FrameLayout> // id : frameLayout
</ConstraintLayout>

 

fragment_1.xml, fragment_2.xml

textview 와 button 한개씩 만든다.

 

Fragment1.java, Fragment2.java (onCreateView)

 

View view = inflater.inflate(R.layout.frag1, container, false);

TextView textView = view.findViewById(R.id.textView);
Button button = view.findViewById(R.id.button);

if (getArguments() != null)
{
    String result = getArguments().getString("fragSend");
    textView.setText(result);
}

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("fragSend","프레그먼트 데이터 전달 1 -> 2");
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        Frag2 frag2 = new Frag2();
        frag2.setArguments(bundle);
        transaction.replace(R.id.frameLayout, frag2);
        transaction.commit(); // 저장
    }
});
return view;

 


 

홍드로이드 Fragment 예제

 

public class MainActivity extends AppCompatActivity {

    Button btn1,btn2,btn3,btn4;

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

        btn1 = (Button)findViewById(R.id.btn_1);
        btn2 = (Button)findViewById(R.id.btn_2);
        btn3 = (Button)findViewById(R.id.btn_3);
        btn4 = (Button)findViewById(R.id.btn_4);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Fragment1 fragment1 = new Fragment1();
                transaction.replace(R.id.frame, fragment1);
                transaction.commit();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Fragment2 fragment2 = new Fragment2();
                transaction.replace(R.id.frame, fragment2);
                transaction.commit();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Fragment3 fragment3 = new Fragment3();
                transaction.replace(R.id.frame, fragment3);
                transaction.commit();
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Fragment4 fragment4 = new Fragment4();
                transaction.replace(R.id.frame, fragment4);
                transaction.commit();
            }
        });
    }
}

 


 

Fragment 를 총 4개 만든다.

Fragment1.java

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}

 


 

activity_main.xml

<RelativeLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="메뉴1" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="메뉴2" />

        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="메뉴3" />

        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="메뉴4" />
    </LinearLayout>
</RelativeLayout>

 


 

총 4개를 만든다.

fragment1.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:text="안드로이드 1"/>

</FrameLayout>

 


 

안드로이드 앱 만들기 #13 (Fragment)
https://www.youtube.com/watch?v=3Th96mVEpyo

 

안드로이드 앱 만들기 #40 Fragment 간 데이터 전송
https://www.youtube.com/watch?v=SK2pLQASmcs

 

홍드로이드 github
https://github.com/hongdroid94/13_Fragment/

 

https://github.com/hongdroid94/40_FragmentBundle

Related posts

Leave a Comment