[英]Android Navigation Drawer Fragment State
我目前正在為我的Android APP使用導航抽屜。 在我的第一個片段中,我有一個使用Facebook的Graph API加載數據的片段。 因此,當我的App首次加載時,它首先轉到第一個片段。
然后,我使用導航抽屜點擊另一個片段並查看它。
最后,我重復使用導航抽屜返回第一個片段並查看它。
我面臨的問題是,如何選擇導航抽屜項目,如何繼續使用已創建的片段,而不是重新創建片段。 我切換片段的代碼如下所示。
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new SelectionFragment();
break;
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new SplashFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
我注意到每次選擇選項時,片段確實存在“新”實例。 我如何實現創建片段實例ONCE並重用它的邏輯,這樣我就不必一遍又一遍地連續加載片段。
對於遇到同樣問題的人,我設法找到了解決方案。
在容器框架中,我將定義我將使用的特定片段視圖,如下所示。
在每個片段視圖中,我將通過“android:name”屬性將其與實際片段本身“鏈接”,如下所示。請注意片段的路徑 ,例如在我的例子中它是com.example .confesssionsrp.SelectionFragment 。
<fragment
android:id="@+id/selectionFragment"
android:name="com.example.confessionsrp.SelectionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在MainActivity(或我們正在查看片段的Activity)中,為每個片段創建變量,如下所示。
然后,在MainActivity的Oncreate (或您的特定活動)中,初始化各種片段,如下所示。
繼續創建一個名為“ ShowFragment ”的新方法,如下所示。
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
if (Session.getActiveSession().isClosed()) {
mDrawerLayout
.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
從那時起,在切換片段時,使用所選片段手動調用“ ShowFragment ”方法,如下所示。
完成所有這些操作,每次我們查看時都不會重置片段,因此是答案的解決方案。感謝迄今為止幫助過我的任何人:)!
我使用以下代碼:
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
if(position==0){// selection of tabs content
fragmentManager
.beginTransaction()
.replace(R.id.container,
SimulatorFragment.newInstance(position + 1)).commit();
}else if(position==1){
fragmentManager
.beginTransaction()
.replace(R.id.container,
HudFragment.newInstance(position + 1)).commit();
}else if(position==2){
// Display the fragment as the main content.
fragmentManager
.beginTransaction()
.replace(R.id.container,
SettingsBasicFragment.newInstance(position +1)).commit();
}else{
}
}
您可以第一次用新實例替換並存儲片段,如果它不為null,則替換為存儲的片段。
該活動必須實現NavigationDrawerFragment.NavigationDrawerCallbacks
片段構造函數和newInstance方法如下所示:
public final class HudFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
* @param simulation
*/
public static HudFragment newInstance(int sectionNumber) {
HudFragment fragment = new HudFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public HudFragment() {
}
要按代碼切換片段,我在NavigationDrawerFragment中使用此方法:
/**
* Select a different section
* @param position
*/
public void select(int position){
selectItem(position);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
第二個選項是從Android SDK提供的navigationDrawer示例開始。 我在創建項目時選擇了該活動模板,幾乎所有前一個答案的代碼都是自動生成的。
如果你想在設備旋轉或類似物之后保留片段,那么你需要保留碎片。 如果沒有,您只需要在變量中保存片段的新實例,並檢查它是否為null以創建新的或使用舊的。
如果有人想要一個不同的方法:你可以在堆棧上找到片段:
// check if this fragment is on the backstack to avoid creating a new one
Fragment foundFragment = fragmentManager.findFragmentByTag("unique_fragment_tag");
if (foundFragment != null) {
fragmentManager.popBackStackImmediate("unique_fragment_tag", 0);
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.