프로그래밍/안드로이드

안드로이드 - 프래그먼트 사용 간단 예제

가카리 2015. 9. 8. 17:25
반응형

 

프래그먼트 간략 설명은 다음 링크를 참조 하자.

 

커니의 안드로이드 프래그먼트 설명

프래그먼트 생명주기 설명

 

 

간단한 예제에 앞서 프로젝트 익스플로러를 보자

 

아래와 같이 자바파일과 xml파일을 2개로 구성함을 잊지 말자.

 

counterfragment.xml 파일

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:background="#ffff00"

>

 

<TextView

android:id="@+id/txtcounter"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#ff0000"

android:textSize="40sp"

android:text="0"

/>

<Button

android:id="@+id/btnincrease"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Increase"

/>

 

</LinearLayout>

 

fragment_test.xml

 

<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"

android:orientation="vertical" >

 

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="아래 영역은 프래그먼트로 생성되었습니다."/>

 

<!-- fragment 선언시 name 패키지명.메인클래스명$fragment 상속받은 클래스명 -->

<fragment

android:name="com.example.ch20_fragment_ex1.FragmentTest$CounterFragment"

android:id="@+id/counterfragment"

android:layout_width="match_parent"

android:layout_height="wrap_content"

></fragment>

 

</LinearLayout>

 

FragmentTest.java

 

package com.example.ch20_fragment_ex1;

 

import android.app.Activity;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.TextView;

 

public class FragmentTest extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_test);

    }

public static class CounterFragment extends Fragment{

      

    

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        //onCreateView 프래그먼트의 UI 처음 그릴때 호출한다.

        //그래서 여기서 XML 이용해서 프래그먼트 UI 구성해준다.

        //container 전개자를 사용하여 xml 가져온다

        View root = inflater.inflate(R.layout.counterfragment, container, false);

        

        Button btnIncrease = (Button)root.findViewById(R.id.btnincrease);

        final TextView textCounter = (TextView)root.findViewById(R.id.txtcounter);

      

        //간단한 버튼 클릭 리스너 달아줌

        btnIncrease.setOnClickListener(new Button.OnClickListener(){

 

            @Override

            public void onClick(View arg0) {

                // TODO Auto-generated method stub

                int count = Integer.parseInt(textCounter.getText().toString());

                textCounter.setText(Integer.toString(count + 1));

            }

            

        });

        

        return root;

    

    }

    

}

      

    

}

 

 

실행 화면

다음과 같이 프래그먼트가 생성됨을 알 수 있다.

 

 

반응형