728x90
https://www.youtube.com/watch?v=SK2pLQASmcs&list=PLC51MBz7PMyyyR2l4gGBMFMMUfYmBkZxm&index=42
FrameLayout이란? 화면이 바뀌는 영역
코드
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/tv_frag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:text="프레그먼트 1"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.156"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btn_move"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="프레그먼트 2로 이동"
app:layout_constraintEnd_toEndOf="@+id/tv_frag1"
app:layout_constraintStart_toStartOf="@+id/tv_frag1"
app:layout_constraintTop_toBottomOf="@+id/tv_frag1" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/tv_frag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:text="프레그먼트 2"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.156"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btn_move"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="프레그먼트 1로 이동"
app:layout_constraintEnd_toEndOf="@+id/tv_frag2"
app:layout_constraintStart_toStartOf="@+id/tv_frag2"
app:layout_constraintTop_toBottomOf="@+id/tv_frag2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment1.java
package com.example.fragmentbundleexample;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment1 extends Fragment {
private View view;
private TextView tv_frag1;
private Button btn_move;
private String result;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //프레그먼트 제일 처음 실행 될때 생성되는 생명 주기
view = inflater.inflate(R.layout.fragment_1, container, false);
tv_frag1 = view.findViewById(R.id.tv_frag1);//fragment에서는 view.해야함
btn_move = view.findViewById(R.id.btn_move);
if (getArguments() != null) // null : 빈 값 (앞에서 클릭 안해서 setArgument 안 됐으면)
{
result = getArguments().getString("fromFrag2"); // 프레그먼트 1로부터 setArgument된 데이터를 받아옵니다.
tv_frag1.setText(result);//TextView에 뿌려줌
}
btn_move.setOnClickListener(new View.OnClickListener() { //프레그먼트 2로 이동
@Override
public void onClick(View v) { //액티비티에서 사용했던 intent랑 비슷하다 사용법이
Bundle bundle = new Bundle(); // 무언가를 담을 준비를 할 수 있는 보따리 or 꾸러미
bundle.putString("fromFrag1", "hessepark 프레그먼트 1"); //putExtra랑 비슷
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();//transaction은 fragment 관리(intent랑 비슷한 것) 값 가져오거나 값 뭐 있는지 검사
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle); //fragment 안에 꾸러미를 넣는다 넣어서 넘겨줌
transaction.replace(R.id.frameLayout, fragment2);//startActivity랑 비슷 (교체할 화면 영역, fragment2로 교체)
transaction.commit(); // 저장
}
});
return view;
}
}
Fragment2.java
package com.example.fragmentbundleexample;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment2 extends Fragment {
private View view;
private TextView tv_frag2;
private Button btn_move;
private String result;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_2, container, false);
tv_frag2 = view.findViewById(R.id.tv_frag2);//fragment에서는 view.해야함
btn_move = view.findViewById(R.id.btn_move);
if (getArguments() != null) // null : 빈 값 (앞에서 클릭 안해서 setArgument 안 됐으면) 즉 값을 받아온 경우에만
{
result = getArguments().getString("fromFrag1"); // 프레그먼트 1로부터 setArgument된 데이터를 받아옵니다.
tv_frag2.setText(result);//TextView에 뿌려줌
}
btn_move.setOnClickListener(new View.OnClickListener() { //프레그먼트 1로 이동
@Override
public void onClick(View v) { //액티비티에서 사용했던 intent랑 비슷하다 사용법이
Bundle bundle = new Bundle(); // 무언가를 담을 준비를 할 수 있는 보따리 or 꾸러미
bundle.putString("fromFrag2", "hessepark 프레그먼트 2"); //putExtra랑 비슷
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();//transaction은 fragment 관리(intent랑 비슷한 것) 값 가져오거나 값 뭐 있는지 검사
Fragment1 fragment1 = new Fragment1();
fragment1.setArguments(bundle); //fragment 안에 꾸러미를 넣는다 넣어서 넘겨줌
transaction.replace(R.id.frameLayout,fragment1);//startActivity랑 비슷 (교체할 화면 영역, fragment2로 교체)
transaction.commit(); // 저장
}
});
return view;
}
}
MainActivity.java
package com.example.fragmentbundleexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//자식 아니라서 getActivity 빼주면 된다.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//transaction은 fragment 관리(intent랑 비슷한 것) 값 가져오거나 값 뭐 있는지 검사
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.frameLayout,fragment1);//startActivity랑 비슷 (교체할 화면 영역, fragment2로 교체)
transaction.commit(); // 저장
}
}
Main에서는 getActivity할 필요 없다. 이미 메인이라서.
728x90
'[Android Studio] (Java)' 카테고리의 다른 글
| [Android Studio] View Binding (뷰 바인딩) (0) | 2023.09.03 |
|---|---|
| [Android Studio] Frame Layout (뷰 끼리 겹치기) (0) | 2023.09.03 |
| [Android Studio] Table Layout (엑셀 표 느낌) (0) | 2023.09.02 |
| [Android Studio] Card View (카드 뷰) (0) | 2023.09.02 |
| [Android Studio] Check Box (옵션 선택 버튼) (0) | 2023.09.02 |