조각은 대체되지 않지만 이전 조각 위에 놓입니다. : <fragment

활동:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

Fragment1 fragment = new Fragment1();
Fragment2 fragment2 = new Fragment2();

transaction.replace(R.id.Fragment1, fragment);
transaction.addToBackStack(null);
transaction.commit();

FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.Fragment1, fragment2);
transaction2.addToBackStack(null);
transaction2.commit();

보기의 코드 :

<fragment
    android:id="@+id/Fragment1"
    android:name="com.landa.fragment.Fragment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/include1" /> 

문제는 콘텐츠가 실제로 교체되지 않는다는 것입니다.

다시 클릭하면 첫 번째 조각이 제대로 표시되지만 (두 번째 조각은 제외) 처음에는 둘 다 표시됩니다 (마지막 조각 만 표시하고 싶습니다).

내가 여기서 무엇을 놓치고 있습니까?



답변

여기서 두 가지 잘못하고 있습니다.

  1. xml레이아웃 파일 에 정적으로 배치 된 조각은 바꿀 수 없습니다 . FrameLayout레이아웃에 컨테이너 (예 : a )를 생성 한 다음을 사용하여 프로그래밍 방식으로 조각을 추가해야합니다 FragmentTransaction.

  2. FragmentTransaction.replace첫 번째 매개 변수로 조각의 ID가 아니라 조각을 포함하는 컨테이너의 ID를 예상합니다. 따라서 첫 번째 조각을 추가 한 컨테이너의 ID로 첫 번째 인수를 전달해야합니다.

자세한 내용 은 이 링크 를 참조하십시오.


답변

비슷한 문제가 있었지만 내 문제는 두 개의 다른 Fragment 관리자를 사용 getSupportFragmentManager()하고 있다는 것입니다 getFragmentManager(). 하나의 조각을으로 추가 한 다음 조각 SupportFragmentManager을으로 바꾸려고 FragmentManager하면 조각이 맨 위에 추가됩니다. 동일한 코드를 FragmentManager사용하고 문제를 처리 하도록 코드를 변경해야했습니다 .


답변

안드로이드 개발자 사이트에서는 FrameLayout런타임에 동적으로 조각을로드 하기 위해 a 를 사용할 것을 제안합니다 . xml 파일에 조각을 하드 코딩했습니다. 따라서이 링크에서 언급 한대로 런타임에 제거 할 수 없습니다.

http://developer.android.com/training/basics/fragments/creating.html

이 링크는 프로그램을 통해 조각을 추가하는 방법을 보여줍니다.

http://developer.android.com/training/basics/fragments/fragment-ui.html


답변

나는 같은 문제가 있었고 모든 답을 보았지만 그들 중 어느 것도 내 실수를 포함하지 않았습니다! 현재 조각을 교체하기 전에 다음과 같이 컨테이너 활동의 xml에 기본 조각을 표시했습니다.

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

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="....ShopFragment"
        android:id="@+id/fragment"
        tools:layout="@layout/fragment_shop" />
</FrameLayout>

그 후, 나는이 통과되었지만 FrameLayout받는 fragmentTransaction.replace()하지만 난 정확한 문제가 있었다. 이전 조각 위에 두 번째 조각이 표시되었습니다.

XML에서 조각을 제거하고 onCreate()다음과 같이 프로그램 시작시 기본 미리보기에 대한 컨테이너 활동 의 메서드 에서 프로그래밍 방식으로 표시하여 문제를 해결 했습니다.

    fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_frame_layout,new ShopFragment());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

및 컨테이너 활동 xml :

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

답변

조각을 교체하기 전에 backStack을 지울 수 있습니다.

    FragmentManager fm = getActivity().getSupportFragmentManager();

    for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
        fm.popBackStack();
    }

    // replace your fragments

답변

<FrameLayout
    android:id="@+id/fragment_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground">
//Add fragment here
</FrameLayout>

android:background="?android:windowBackground">조각이있는 컨테이너에 추가 합니다. 제 경우에는 FrameLayout도움이 될을 추가했습니다 .


답변

조각을 사용하는 대신 컨테이너 사용

<FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!" />

이제 MainActivity에서

if(condition)
    getFragmentManager().beginTransaction().replace(R.id.container,new FirstFragment()).commit();
else
    getFragmentManager().beginTransaction().replace(R.id.container, new SecondFragment()).commit();

프래그먼트 클래스는 ‘android.app.Fragment’를 가져와야합니다. “support.v4″또는 “원본”구현을 사용하고 두 가지를 혼합하지 마십시오. 나는 그들을 혼합했기 때문에이 문제가 발생했습니다.