簡體   English   中英

前一個片段在新片段下可見

[英]Previous fragment visible under the new fragment

我有一個選項卡 + ViewPager布局,在這些選項卡之一中我有一個列表視圖。 當我在 onclick 上替換該列表片段時,我仍然可以在新片段下看到舊片段。 看:

在此處輸入圖像描述

代碼:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
HallsInStateFragment hallsForState = new HallsInStateFragment();        
transaction.replace(R.id.container, hallsForState);
transaction.addToBackStack(null);
transaction.commit();

其中R.id.container是視圖中的 FrameLayout。

當需要從父視圖中刪除所有視圖時,您需要在片段的onCreateView()方法中的容器中調用removeAllViews()

這是代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

container.removeAllViews(); // Inflate the layout for this fragment
 return inflater.inflate(R.layout.fragment_example, container, false); 
}

而不是 R.id.container 把片段的 id 像這樣: ((ViewGroup)getView().getParent()).getId() 實際上它不是在替換片段,而是在替換之前的布局,即 FrameLayout。 它對我有用,我希望它也適用於您的情況。

將此添加到兩個片段父布局

android:background="@android:color/white"

您正在替換的一個和您將替換的一個。

片段的 UI 是活動視圖層次結構的一部分。 因此,如果您在 onCreateView() 方法中創建了視圖,那么您可以使用 ViewGroup 容器來擴展您的布局。 此容器保留對片段視圖的引用。 嘗試覆蓋片段的 onDestroyView() 方法並從父級中刪除所有視圖:

@Override
public void onDestroyView() {
    //mContainer.removeAllViews();
    ViewGroup mContainer = (ViewGroup) getActivity().findViewById(R.id.container);
    mContainer.removeAllViews();
    super.onDestroyView();
}

你的片段應該像這樣在FrameLayout加載

    <FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground" />

並且您的片段應該通過此功能添加/加載到此frameLayout

    private fun switchFragment(
    fragment: Fragment,
    addToBackstack: Boolean
) {
    //check new fragment is alredy loaded currently, then return
    val myFragment =
        supportFragmentManager.fragments.lastOrNull()//return current visible fragment or null
    if (myFragment != null && fragment::class == myFragment::class) {
        return
    }

    val fragmentManager = supportFragmentManager
    val transaction = fragmentManager.beginTransaction()
    //transaction.add(R.id.frameLayout, fragment, fragment.javaClass.name)
    transaction.replace(
        R.id.frameLayout,
        fragment,
        fragment.javaClass.name
    )//using replace will make sure that the previous fragment won't be visible from new fragment
    if (addToBackstack) {
        transaction.addToBackStack(fragment.javaClass.name)
    }
    transaction.commit()
}

所以最初,你的第一個片段應該像這樣加載,

switchFragment(HomeFragment(), false)

然后當你從底部導航視圖或導航抽屜中選擇其他片段時,像這樣調用這個函數

switchFragment(MyProfileFragment(), true)

在我的案例中,發生這種情況是因為我使用 static 片段作為

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

<fragment
    android:name="com.example.android.FooFragment"
    android:id="@+id/fooFragment"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" />

</LinearLayout>

其中 foofragment 是初始片段,然后嘗試用其他片段替換片段,以便兩個片段重疊。

相反,問題解決了,當我使用動態鏈接代替 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" >

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

</LinearLayout>

然后動態添加 fragment-1 並替換為 fragment-2,對我來說工作正常。

I did not find answer earlier, hence posting this solution.

在根視圖中添加clickable = true並添加替換當前片段的片段的背景顏色(均在 xml 中)。 它只是解決方法的修復

最好的代碼。 顯然並沒有使用另一個設備內存。 在您的 Activity onCreate 或添加片段之前添加以下代碼:

for (Fragment fragment : getSupportFragmentManager().getFragments()) {
    if (fragment instanceof NavigationDrawerFragment) {
        continue;
    }
    else if (fragment != null) {
        getSupportFragmentManager().beginTransaction().remove(fragment).commit();
    }
}

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM