기본 콘텐츠로 건너뛰기

Android, simple Fragment sample

* Main ----------------------------------------------------------

***** xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">


        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="fragment1"            android:id="@+id/button1"            android:layout_weight="1"            android:onClick="selectFrag" />

        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="fragment2"            android:id="@+id/button2"            android:layout_weight="1"            android:onClick="selectFrag" />
    </LinearLayout>


    <fragment        android:id="@+id/fragment_place"        android:name="[Fragment1 class]"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:layout="@layout/fragment1" />


</LinearLayout>

***** java
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view){
        Fragment fr;

        if(view == findViewById(R.id.button1)){
            fr = new Fragment1();
        }else {
            fr = new Fragment2();
        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();
    }
}

* Fragment1 ----------------------------------------------------------

***** fragment1.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff9b9b">

</LinearLayout>

***** java
public class Fragment1 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}

* Fragment2 ----------------------------------------------------------

***** fragment2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#7dd163">

</LinearLayout>

***** java
public class Fragment2 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}



ref :
http://yoo454.tistory.com/entry/Fragment-%EA%B0%84%EB%8B%A8-%EC%98%88%EC%A0%9C
http://novafactory.net/archives/1837


댓글

이 블로그의 인기 게시물

Android, 서명이 충돌하는 같은 이름의 기존 패키지가 이미 설치되어 있습니다.

Nexus 7 1세대 4.4 Kitkat 앱이 설치되지 않습니다. 서명이 충돌하는 같은 이름의 기존 패키지가 이미 설치되어 있습니다. 위의 증상으로 apk로 앱이 설치되지 않음. 설치되어있는 앱을 지우고 다시 설치하려고 해도 되지 않음. 구글 플레이에서 설치할때도 비활성->활성되는듯 설치 딜레이가 없었음. 앱을 삭제할시 해당사용자에서 링크만 삭제되는 듯이 보임. 어플리케이션 리스트의 가장밑에 설치하려는 앱의 아이콘을 발견.(삭제됨이라고 표시) 그 앱의 메뉴를 열어 "모든 사용자에서 삭제"를 선택하면 앱의 아이콘이 사라진다. 그후 apk를 설치하면 정상으로 진행.