2016-03-22 76 views
0

我已經能夠使用PreferenceFragment實現設置頁面。這顯示得很好,直到我導航到另一個頁面。設置頁面仍顯示在其他頁面的頂部,我不知道如何完全替換它。PreferenceFragment與片段佈局重疊

此圖片是我的意思的一個例子。我已經通過導航抽屜從設置頁導航到主頁。我想我錯過了replace()方法的一些東西,但我不知道是什麼。

enter image description here

AccountSettings.java

public class AccountSettings extends PreferenceFragment { 

Activity mActivity; 

private static final String ARG_SECTION_NUMBER = "section_number"; 

public static AccountSettings newInstance(int sectionNumber) { 
    AccountSettings fragment = new AccountSettings(); 
    Bundle args = new Bundle(); 
    args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
    fragment.setArguments(args); 
    return fragment; 
} 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.settings); 

} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    getView().setBackgroundColor(Color.WHITE); 
    getView().setClickable(true); 
} 


@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    mActivity = activity; 

} 


@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 


@Override 
public void onDetach() { 
    super.onDetach(); 
} 


public void restoreActionBar() { 
    ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBar.setTitle("Account Settings"); 
} 

public AccountSettings() { 

} 

} 

MainPage.java(NAV抽屜)

public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    if (position == 0) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, PlaceholderFragment.newInstance(1)) 
       .commit(); 

    } else if (position == 1) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, Search.newInstance(2)) 
       .commit(); 

    } 

    else if (position == 2) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, Favourites.newInstance(3)) 
       .commit(); 
    } 

    else if (position == 3) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, History.newInstance(4)) 
       .commit(); 

    } 

    else if (position == 4) { 
     getFragmentManager().beginTransaction() 
       .replace(R.id.container, AccountSettings.newInstance(5)) 
       .commit(); 

    } 

} 

的settings.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
android:key="prefs"> 

<PreferenceCategory android:title="@string/pref_search_category" > 
<CheckBoxPreference 
    android:defaultValue="false" 
    android:key="prefDateTime" 
    android:summary="@string/pref_night_day_description" 
    android:title="@string/pref_night_day" > 
</CheckBoxPreference> 
</PreferenceCategory> 

</PreferenceScreen> 

activity_main_page.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainPage"> 

<FrameLayout android:id="@+id/container" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".AccountSettings" 
    /> 

<fragment android:id="@+id/navigation_drawer" 
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:name="com.example.laptop.whatsfordinner.NavigationDrawerFragment" 
    tools:layout="@layout/fragment_navigation_drawer" 
    /> 

請幫幫忙!

回答

1

在片段oncreateView方法膨脹所述片段的視圖之前加入這一行

container.removeAllViews() 

。這幫助我在this problem

+0

哇只是一條線有很大的區別!謝謝! – Theman

+0

很高興我的建議幫助你:) –