【发布时间】:2014-07-19 03:23:22
【问题描述】:
我正在使用支持操作栏。我也在使用片段。通常,我使用导航抽屉更改片段并且一切正常。每个片段都能够对操作栏项目进行更改。当我更改为新片段时,更改(新按钮)将被删除。
这是我的问题。在一个实例中,我替换了片段并将现有片段添加到后堆栈。当我回到前一个片段时,操作栏仍然有前一个片段中的项目。
示例: 1) Fragment 1 加载 Fragment 2 并将 Fragment 1 放在后栈中 2) Fragment 2 在操作栏中放置了几个新按钮 3) 片段 2 退出,现在我看到片段 1 和 2 中的操作栏项目
我对不同位置的其他 2 个片段执行相同的基本操作,并且没有相同的问题。我能弄清楚这两种实现之间的唯一区别是第二个片段与操作栏交互的方式。
在工作情况下,片段 2 加载项在 onCreateMenuOptions 中膨胀菜单。这很好用。
在不工作的情况下,片段 2 不会膨胀菜单而是使用自定义视图:
// Set action bar custom view and display options.
MINMainActivity.getSharedInstance().getSupportActionBar().setCustomView(R.layout.custom_action_bar_viewer);
MINMainActivity.getSharedInstance().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);
MINMainActivity.getSharedInstance().getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));
这是我能找到的唯一区别。我错过了什么吗? (显然)但问题是什么???
用于从片段 1 加载片段 2 的代码是:
MINMainActivity.getSharedInstance().mDrawerToggle.setDrawerIndicatorEnabled(false);
// Load the PDF fragment
MINPDFTronFragment fragment = new MINPDFTronFragment();
fragment.albumItem = albumItem;
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
片段2使用的onCreate代码是:
// ***************************************************************************************************
//
// onCreate
//
// ***************************************************************************************************
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//MINMainActivity.getSharedInstance().supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY);
setRetainInstance(true);
setHasOptionsMenu(true);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
新编辑:
这是在操作栏上工作的两种方法。这里的第一部分是 Fragment 1 的代码。Fragment 1 是启动 Fragment 2 并将自身置于 backstack 上的 Fragment:
// ***************************************************************************************************
//
// onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
mMenu = menu;
// Inflate GridView menu
inflater.inflate(R.menu.gridview_combined_menu, menu);
// Gridview share menu
MenuItem shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
setGridViewShareIntent(null); // TODO is this needed?
super.onCreateOptionsMenu(menu, inflater);
}
// ***************************************************************************************************
//
// onPrepareOptionsMenu
//
// ***************************************************************************************************
@Override
public void onPrepareOptionsMenu(Menu menu)
{
setOptionMenuItems();
super.onPrepareOptionsMenu(menu);
}
下面是 Fragment 2 中相同的方法(Fragment 1 启动的 Fragment:
// ***************************************************************************************************
//
// onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
mMenu = menu;
super.onCreateOptionsMenu(menu, inflater);
}
// ***************************************************************************************************
//
// onPrepareOptionsMenu
//
// ***************************************************************************************************
@Override
public void onPrepareOptionsMenu(Menu menu)
{
MINMainActivity MINMainActivity = (MINMainActivity) getActivity();
boolean drawerIsOpen = MINMainActivity.mDrawerLayout.isDrawerOpen(MINMainActivity.mDrawerList);
if(drawerIsOpen)
{
}
else
{
}
}
super.onPrepareOptionsMenu(menu);
}
如您所见,Fragment 2 并没有使用这些方法制作模组。下面是片段 2 中操作操作栏的代码:
// ***************************************************************************************************
//
// onCreateView
//
// ***************************************************************************************************
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
initViewerLayout(inflater, container, savedInstanceState);
}
// ***************************************************************************************************
//
// initViewerLayout
//
// ***************************************************************************************************
private void initViewerLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Initialize the library
try
{
PDFNet.initialize(MINMainActivity.getSharedInstance(), R.raw.pdfnet);
}
catch (PDFNetException e)
{
// Do something...
e.printStackTrace();
}
// Inflate the view and get a reference to PDFViewCtrl
pageView = inflater.inflate(R.layout.pdf_viewer, container, false);
// Set action bar custom view and display options.
MINMainActivity.getSharedInstance().getSupportActionBar().setCustomView(R.layout.custom_action_bar_viewer);
MINMainActivity.getSharedInstance().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);
MINMainActivity.getSharedInstance().getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));
// Initialize action bar buttons (we are using a custom view, so we
// need to set click listeners for each item.
mButtonViewMode = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_viewmode);
mButtonViewMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewModePickerDialogFragment dialog = new ViewModePickerDialogFragment();
dialog.mListener = MINPDFTronFragment.this;
dialog.show(MINMainActivity.getSharedInstance().getSupportFragmentManager(), "view_mode_picker");
stopHideToolbarsTimer();
}
});
updateButtonViewModeIcon();
mButtonOutline = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_outline);
mButtonOutline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = MINMainActivity.getSharedInstance().getSupportFragmentManager();
// Creates the dialog in full screen mode
mBookmarksDialog = new BookmarksDialogFragment(mPdfViewCtrl, mBookmarkDialogCurrentTab);
mBookmarksDialog.setBookmarksDialogFragmentListener(MINPDFTronFragment.this);
mBookmarksDialog.mOutlineDialogFragmentListener = MINPDFTronFragment.this;
mBookmarksDialog.mAnnotationDialogFragmentListener = MINPDFTronFragment.this;
mBookmarksDialog.setStyle(DialogFragment.STYLE_NO_TITLE, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
mBookmarksDialog.show(fragmentManager, "bookmarks_dialog");
stopHideToolbarsTimer();
// Example of how to show the OutlineDialogFragment as a dialog
//OutlineDialogFragment outlineDialogFragment = new OutlineDialogFragment(mPdfViewCtrl);
//outlineDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light);
//outlineDialogFragment.show(fragmentManager, "outline_dialog");
// Example of how to show the AnnotationDialogFragment as a dialog
//AnnotationDialogFragment annotDialog = new AnnotationDialogFragment(mPdfViewCtrl);
//annotDialog.setStyle(DialogFragment.STYLE_NO_TITLE, android.support.v7.appcompat.R.style.Theme_Base_AppCompat_Light);
//annotDialog.show(fragmentManager, "annotation_dialog");
}
});
mSearchView = (SearchView) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_search);
mSearchView.setFocusable(false);
mSearchView.setFocusableInTouchMode(false);
// Let's get the internal button of the search view widget to change some properties.
ImageView searchButton = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchButton.setBackgroundDrawable(mSearchView.getContext().getResources().getDrawable(R.drawable.controls_annotation_toolbar_tool_bg));
searchButton.setImageDrawable(mSearchView.getContext().getResources().getDrawable(R.drawable.ic_action_search));
mSearchView.setOnQueryTextListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SearchView searchView = (SearchView) view;
if (searchView.isIconfiedByDefault() && !searchView.isIconified()) {
AnalyticsHandler.getInstance().sendEvent("[Viewer] Find Text clicked");
// Search has expanded
startSearchMode();
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
exitSearchMode();
return false;
}
});
mButtonAnnotToolbar = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_tools);
mButtonAnnotToolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnnotationToolbar = new AnnotationToolbar(MINApplication.getAppContext(), pageView, mToolManager);
mAnnotationToolbar.setButtonStayDown(SettingsManager.getContinuousAnnotationEdit(MINApplication.getAppContext()));
if (SettingsManager.getContinuousAnnotationEdit(MINApplication.getAppContext())) {
AnalyticsHandler.getInstance().sendEvent("[General] Continuous Annotation Edit enabled");
}
mAnnotationToolbar.show();
AnalyticsHandler.getInstance().sendEvent("[Annotation Toolbar] Annotation Toolbar opened");
}
});
mButtonShare = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_share);
mButtonShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (MiscUtils.isKitKat()) {
// If KitKat or higher, we show the Share/Print option
SharePickerDialogFragment dialog = new SharePickerDialogFragment();
dialog.mListener = MINPDFTronFragment.this;
dialog.show(MINMainActivity.getSharedInstance().getSupportFragmentManager(), "share_mode_picker");
} else {
MiscUtils.sharePdfFile(MINApplication.getAppContext(), albumItem);
AnalyticsHandler.getInstance().sendEvent("[Viewer] Share clicked");
}
}
});
// Search prev/next buttons.
mButtonSearchNext = (ImageButton) pageView.findViewById(R.id.floating_button_next);
mButtonSearchNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchUp = false;
findText();
}
});
mButtonSearchPrev = (ImageButton) pageView.findViewById(R.id.floating_button_prev);
mButtonSearchPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchUp = true;
findText();
}
});
// PDFViewCtrl settings
mPdfViewCtrl = (CompleteReaderPDFViewCtrl) pageView.findViewById(R.id.pdfviewctrl);
mPdfViewCtrl.setUrlExtraction(true);
mPdfViewCtrl.setupThumbnails(true, true, true, 0, 50 * 1024 * 1024, 0.1);
mPdfViewCtrl.setPageViewMode(PDFViewCtrl.PAGE_VIEW_FIT_PAGE);
mPdfViewCtrl.setPageRefViewMode(PDFViewCtrl.PAGE_VIEW_FIT_PAGE);
mPdfViewCtrl.setHighlightFields(true);
mPdfViewCtrl.setZoomLimits(PDFViewCtrl.ZOOM_LIMIT_RELATIVE, 1.0, 20.0);
updateViewMode();
mSeekBar = (ThumbnailSlider) pageView.findViewById(R.id.thumbseekbar);
// We pass a reference of the PDFViewCtrl to the slider so it can
// interact with it (know number of pages, change pages, get thumbnails...
// At this point no doc is set and the slider has no enough data
// to initialize itself. When a doc is set we need to reset its data.
mSeekBar.setPDFViewCtrl(mPdfViewCtrl);
mPageNumberIndicator = (TextView) pageView.findViewById(R.id.page_number_indicator_current_page);
mSearchProgressDialog = new ProgressDialog(getActivity());
mSearchProgressDialog.setMessage(getResources().getString(R.string.search_in_progress));
mSearchProgressDialog.setIndeterminate(true);
mSearchProgressDialog.setCancelable(true);
mSearchProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mPdfViewCtrl.cancelFindText();
}
});
mDownloadDocumentDialog = new ProgressDialog(getActivity());
mDownloadDocumentDialog.setMessage(getResources().getString(R.string.download_in_progress_message));
mDownloadDocumentDialog.setIndeterminate(true);
mDownloadDocumentDialog.setCancelable(true);
mDownloadDocumentDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MINApplication.getAppContext(), R.string.download_cancelled_message, Toast.LENGTH_SHORT).show();
//TODO finish();
}
});
}
【问题讨论】:
标签: android android-fragments android-actionbar-compat