[챕터 2-4-1] 프래그먼트

2020. 2. 14. 00:00Android/Android 챕터 2-4

반응형

결과화면

 

화면전환 버튼을 누르면 메뉴 프래그먼트 화면 띄움


프래그먼트 생성

 


프로그먼트 명 : MainFragment

 

 




 


MainFragment.java 

 

더보기
package com.example.myfragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}

 


MainFragment.java

 

 


activity_main.xml

 


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

    <fragment
        android:id="@+id/mainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.myfragment.MainFragment" />

</FrameLayout>

 


 

 


MainActivity.java 복사해서

MenuFragment.java 생성

 

 

fragment_main.xml 복사해서

fragment_menu.xml 생성

 



 

더보기
package com.example.myfragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public class MenuFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_menu, container, false);
    }
}

 


MainFragment.java

더보기
package com.example.myfragment;

import android.content.Context;
import android.net.Uri;
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 v) {
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.onFragementChanged(1);
            }
        });

        return rootView;
    }

}

 


 

 

반응형

'Android > Android 챕터 2-4' 카테고리의 다른 글

[챕터 2-4-6] 뷰 페이저  (0) 2020.02.16
[챕터 2-4-5] 탭2  (0) 2020.02.16
[챕터 2-4-4] 탭  (0) 2020.02.16
[챕터 2-4-3] 옵션메뉴  (0) 2020.02.15
[챕터 2-4-2] 프래그먼트 2  (0) 2020.02.15