2014-03-02 4 views
0

두 개의 단추와 조각으로 구성된보기가 있습니다. button1을 누르면 조각 하나 (텍스트보기)가 표시됩니다. 버튼 2를 누르면 조각 2 (텍스트보기)가 표시됩니다. 문제는 단편 중 하나만 보여 주길 바랍니다. 버튼을 앞뒤로 누르면 프래그먼트 1과 프래그먼트 1 사이가 전환됩니다.하나를 표시하려고 할 때 두 개의 조각 표시

왜 그럴까요? 하나의 조각 만 표시되도록이 문제를 어떻게 해결할 수 있습니까? 당신은 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" > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Fragment No.1" 
     android:onClick="selectFrag" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="selectFrag" 
     android:text="Fragment No.2" /> 

    <fragment 
     android:name="com.example.fragmentstest.FragmentOne" 
     android:id="@+id/fragment_place" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 



</LinearLayout> 

답변

2

: 여기

package com.example.fragmentstest; 

import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 


public class MainActivity extends Activity { 

    @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.button2)) { 
      fr = new FragmentTwo(); 


     }else { 
      fr = new FragmentOne(); 

     } 

     FragmentManager fm = getFragmentManager(); 

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

    } 
} 

는 XML-클래스입니다 : 여기

는 자바 클래스입니다. 코드를 통해 추가 된 단편은 제거/대체 할 수 있습니다. 따라서 컨테이너 (예 : FrameLayout)를 지정하고이 컨테이너의 조각을 코드에서 추가/바꾸기 :

<?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" > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Fragment No.1" 
     android:onClick="selectFrag" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="selectFrag" 
     android:text="Fragment No.2" /> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 




public void selectFrag(View view) { 
    Fragment fr; 


    if(view == findViewById(R.id.button2)) { 
     fr = new FragmentTwo(); 


    }else { 
     fr = new FragmentOne(); 

    } 

    FragmentManager fm = getFragmentManager(); 

    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container, fr); 
    fragmentTransaction.commit(); 

} 
관련 문제