簡體   English   中英

片段未替換父活動的容器布局

[英]Fragment not replacing parent activity's container layout

我的Android應用程序中有一個活動,其中我在框架布局中放置了RecyclerView窗口小部件。 框架布局充當父容器。

<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.asad.comppecv2.AdminActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/default_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />

<FrameLayout
    android:id="@+id/container_body"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_below="@+id/default_toolbar">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_below="@id/default_toolbar"
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:scrollbars="vertical" />
</FrameLayout>
</RelativeLayout>

這是我在活動中實例化RecyclerView的代碼。

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerAdapter = new RecyclerAdapter(Participant_Data_List,getApplicationContext());
recyclerView.setAdapter(recyclerAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

該活動包含一個導航抽屜,從中可以加載不同的片段。

我想要的是,當片段加載時,它將替換“幀布局”的內容。 這是負責加載片段的代碼。

private void initiateFragment(){
    if (fragment != null) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.addToBackStack(null);
        ft.replace(R.id.container_body, fragment);
        ft.commit();
    }
}

這是我的活動的圖片,活動活躍時默認情況下實例化RecyclerView。

在此處輸入圖片說明

這是我要加載並替換回收器視圖的片段的圖片。

在此處輸入圖片說明

但是,當我加載此片段而不是替換它時,它在回收器視圖上重疊。

在此處輸入圖片說明

任何幫助,將不勝感激!

更新#2

這是負責處理片段的代碼塊

public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
result.setSelection(position,false);
clearStack();
switch (position) {
    case 1:
        fragment = new admin_fragment_data_entry();
        break;
    case 2:
        fragment = new admin_fragment_data_edit();
        break;
    case 3:
        fragment = new admin_fragment_judge_data();
        break;
    case 4:
        fragment = new admin_fragment_schedule();
        break;
}
initiateFragment();

return false;
}


private void clearStack(){
int count = getSupportFragmentManager().getBackStackEntryCount();
while(count > 0){
    getSupportFragmentManager().popBackStack();
    count--;
}

嘗試這個:

private void initiateFragment(){
    if (fragment != null) {
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.container_body);
        frameLayout.removeAllViews();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.addToBackStack(null);
        ft.replace(R.id.container_body, fragment);
        ft.commit();
    }
}

暫無
暫無

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

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