프로그래밍/안드로이드

안드로이드 - 프래그먼트를 이용한 간단한 사전 만들기

가카리 2015. 9. 21. 22:10
반응형

Fragment를 이용해서 간단한 사전을 만들어 보자.

이번 예제는 다음과 같이 3개의 파일을 만들어야한다.

 

activity_word_list_fragment.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="horizontal">

    <fragment

     android:name="com.example.wordlistfragment.WordListFragment"

     android:id="@+id/wordlist"

     android:layout_width="0dp"

     android:layout_weight="1"

     android:layout_height="match_parent"

     />

    <TextView

     android:id="@+id/worddesc"

     android:layout_width="0dp"

     android:layout_weight="1"

     android:layout_height="match_parent"

     android:textColor="#ff0000"

     android:text="HELLO"

     />

</LinearLayout>

 

WordListFragment.java

 

package com.example.wordlistfragment;

 

import android.app.Activity;

import android.app.ListFragment;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

public class WordListFragment extends ListFragment{

    OnWordChangedListener mHost;

    

    public static String[] WORDS = {

        "1", "2", "3", "4", "5", "6"

    };

    

    public static String[] DESC = {

        "숫자1", "숫자2", "숫자3", "숫자4", "숫자5", "숫자6"

 

    };

    

    public interface OnWordChangedListener{

        public void onWordChanged(int index);

    }

    

    //액티비티에 프래그먼트가 처음 부착될 호출된다.

    public void onAttach(Activity activity){

        super.onAttach(activity);

        

        try{

            //액티비티가 이벤트 인터페이스를 확실히 구현하였는지 확인한다.

            mHost = (OnWordChangedListener)activity;

        }catch(ClassCastException e){

            throw new ClassCastException("activity must implement OnWordChanged");

        }

    }

 

    //액티비티가 완전히 초기화되고 프래그먼트의 레이아웃이 완성되었을때 호출

    public void onActivityCreated(Bundle savedInstanceState){

        super.onActivityCreated(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),

                android.R.layout.simple_list_item_activated_1, WORDS));

        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    }

    

    //리스트 아이템이 클릭됬을 호출

    public void onListItemClick(ListView l, View v, int position, long id){

        l.setItemChecked(position, true);

        mHost.onWordChanged(position);

    }

    

}

 

 

WordList.java

 

package com.example.wordlistfragment;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class WordList extends Activity implements WordListFragment.OnWordChangedListener{

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_word_list_fragment);

    }

    

    //인터페이스 구현.

    public void onWordChanged(int index){

        TextView worddesc = (TextView)findViewById(R.id.worddesc);

        worddesc.setText(WordListFragment.DESC[index]);//여기서 사전 정보를 출력함

    }

}

 

 

실행 화면

 

 

반응형