| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
													Tags
													
											
												
												- 웹브라우저 수용도
 - 키-값 데이터베이스
 - SQL입문
 - 크롤링
 - 숫자형식오류
 - 배열 3요소
 - html
 - 크롤링 오류
 - HTML역사
 - 예제
 - 함수 선언
 - 예외
 - 웹 브라우저 전쟁
 - DoitSQL
 - 자바 예외
 - 배열 예제
 - R1C3
 - 생성자
 - DoitSQL입문
 - SQL
 - 함수
 - 자바 오류
 - dbms
 - 숫자 형식
 - DoIt
 - 우아한테크
 - 페이지분석
 - 데이터베이스
 - Doit입문SQL
 - 자바
 
													Archives
													
											
												
												- Today
 
- Total
 
반응형
    
    
    
  | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
													Tags
													
											
												
												- 웹브라우저 수용도
 - 키-값 데이터베이스
 - SQL입문
 - 크롤링
 - 숫자형식오류
 - 배열 3요소
 - html
 - 크롤링 오류
 - HTML역사
 - 예제
 - 함수 선언
 - 예외
 - 웹 브라우저 전쟁
 - DoitSQL
 - 자바 예외
 - 배열 예제
 - R1C3
 - 생성자
 - DoitSQL입문
 - SQL
 - 함수
 - 자바 오류
 - dbms
 - 숫자 형식
 - DoIt
 - 우아한테크
 - 페이지분석
 - 데이터베이스
 - Doit입문SQL
 - 자바
 
													Archives
													
											
												
												- Today
 
- Total
 
프로그래밍
[Do it! 안드로이드 앱] 자바) 버튼으로 프래그먼트 교체하기 본문
728x90
    
    
  반응형
    
    
    
  둘째 마당 05 프래그먼트 이해하기_01
실행화면

디자인



프로젝트 설명
[메뉴 화면으로] 버튼을 누르면 메뉴 프래그먼트로 [메인 화면으로] 버튼을 누르면 메인 프래그먼트로 이동한다
※ 메뉴 프래그먼트의 바탕색은 주황색이다
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/container">
    <fragment
        android:id="@+id/mainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.sample.samplefragment.MainFragment"/>
</FrameLayout>
MainActivity.java
package com.sample.samplefragment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    MainFragment mainFragment;
    MenuFragment menuFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainFragment);
        menuFragment = new MenuFragment();
    }
    public void onFragmentChanged(int index){
        if (index == 0) {
            getSupportFragmentManager().beginTransaction().replace(R.id.container,
                    menuFragment).commit();
        } else if (index == 1){
            getSupportFragmentManager().beginTransaction().replace(R.id.container,
                    mainFragment).commit();
        }
    }
}
Fragment_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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainFragment"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:text="메인 프레그먼트"/>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="메뉴 화면으로"
        android:textSize="20dp"/>
</LinearLayout>
MainFragment.java
package com.sample.samplefragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_main,
                container, false);
        Button button = rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity activity = (MainActivity) getActivity();
                activity.onFragmentChanged(0);
            }
        });
        return rootView;
    }
    }
Fragment_menu.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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MenuFragment"
    android:orientation="vertical"
    android:background="@android:color/holo_orange_light">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:text="메뉴 프레그먼트"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="메인 화면으로"
        android:textSize="20dp"/>
</LinearLayout>
MenuFragment.java
package com.sample.samplefragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MenuFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_menu,
                container, false);
        MainActivity activity = (MainActivity) getActivity();
        Button button2 = rootView.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onFragmentChanged(1);
            }
        });
        return rootView;
    }
}728x90
    
    
  반응형
    
    
    
  '안드로이드 > Do it! 안드로이드 앱프로그래밍' 카테고리의 다른 글
| [Do it! 안드로이드 앱] 자바) 프래그먼트 이용하여 이미지뷰어 만들기 (0) | 2022.05.23 | 
|---|---|
| [Do it! 안드로이드 앱] 자바) 세 개 이상의 화면 만들어 전환하기 (0) | 2022.05.23 | 
| [Do it! 안드로이드 앱] 자바) 로그인 화면과 메뉴 화면 전환하기 (2) | 2022.05.20 | 
| [Do it! 안드로이드 앱] 자바) 두 종류의 버튼 모양 만들기 (0) | 2022.05.18 | 
| [Do it! 안드로이드 앱] 자바) SMS 입력 화면 만들고 글자의 수 표시하기 (0) | 2022.05.16 | 
			  Comments