gpt4 book ai didi

android - 向上导航(操作栏的后退箭头)不适用于 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:25 26 4
gpt4 key购买 nike

我将抽屉布局作为 Activity 的基本布局,我正在替换此抽屉布局内框架上的两个 fragment 。第一个 fragment 未添加到 fragment 的返回堆栈中。我在我的 Activity 中显示汉堡包图标(我还想要我的第一个 fragment 中的抽屉菜单)。在第二个 fragment 中,我通过 mActionBarDrawerToggle.setDrawerIndicatorEnabled(false) 禁用了汉堡包图标,并使用 actionBar.setDisplayHomeAsUpEnabled(true) 启用了后退按钮。

在第一个 fragments onResume 中,我通过 mActionBarDrawerToggle.setDrawerIndicatorEnabled(true)` 启用了汉堡包图标,这样当用户从第二个 fragment 按下后退按钮(硬件和操作栏的向上按钮)时,用户将返回到第一个 fragment 和汉堡包图标将被启用。一切正常,只是我无法从第二个 fragment 操作栏后退按钮返回。我无法点击它。下面是我的代码:-

Activity 代码

if (Utility.isLargeScreen(this))
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

mHiddenGemsApplication = (HiddenGemsApplication) getApplication();

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initViews();

setSupportActionBar(mToolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
mTextViewActionBarTitle.setText(getString(R.string.app_name));

mActionBarDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this, mDrawerLayout, mToolbar, R.string.open_drawer, R.string.close_drawer) {

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};

mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

mActionBarDrawerToggle.syncState();

mFragmentManager = getSupportFragmentManager();

replaceFragment(new CategoryFragment(), getString(R.string.app_name), CategoryFragment.TAG);

@Override
public void onBackPressed() {

if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
if (mFragmentManager.getBackStackEntryCount() > 0) {
mFragmentManager.popBackStack();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

public void replaceFragment(Fragment fragment, String actionBarTitle, String tag) {

if (mFragmentManager == null)
return;

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment, tag);
if (!tag.equals(CategoryFragment.TAG))
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();

setActionBarTitle(actionBarTitle);
}

public void setActionBarTitle(String actionBarTitle) {
if (!TextUtils.isEmpty(actionBarTitle))
mTextViewActionBarTitle.setText(actionBarTitle);
}

public void setDrawerIndicatorEnabled(boolean value) {
if (mActionBarDrawerToggle != null) {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(value);
}
}

Activity XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="nirvaniclabs.com.hiddengems.activities.HomeActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
android:id="@+id/toolbarlayout"
layout="@layout/toolbar_layout" />

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbarlayout" />
</RelativeLayout>

<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navigation_items" />

</android.support.v4.widget.DrawerLayout>

第一个 fragment :-

 private Button mButtonTemp;
private AppCompatActivity mActivity;
public static String TAG = "CategoryFragment";

public CategoryFragment() {
// Required empty public constructor
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

if (context instanceof Activity)
mActivity = (AppCompatActivity) context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View viewGroup = inflater.inflate(R.layout.fragment_category, container, false);
initViews(viewGroup);

mButtonTemp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

((HomeActivity) mActivity).replaceFragment(new TripListFragment(), "Trip Fragment", TripListFragment.TAG);
}
});

return viewGroup;
}

private void initViews(View viewGroup) {
mButtonTemp = (Button) viewGroup.findViewById(R.id.btn_temp);
}

@Override
public void onResume() {
super.onResume();
((HomeActivity) mActivity).setDrawerIndicatorEnabled(true);
((HomeActivity) mActivity).setActionBarTitle(getString(R.string.app_name));
}

第二个 fragment

  private AppCompatActivity mActivity;
public static String TAG = "TripListFragment";

public TripListFragment() {
// Required empty public constructor
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity)
mActivity = (AppCompatActivity) context;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setHasOptionsMenu(true);
ActionBar actionBar = mActivity.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_trip_list, container, false);
}


@Override
public void onResume() {
super.onResume();
((HomeActivity) mActivity).setDrawerIndicatorEnabled(false);
}

此外,当我在第二个 fragment 中时,我可以滑动并查看抽屉菜单。我不想要这种行为,抽屉菜单应该只在 fragment 1 中打开。

如果我的代码有任何问题,请告诉我。

最佳答案

终于有了答案。在我的场景中,我通过 mActionBarDrawerToggle.setDrawerIndicatorEnabled(false); 禁用抽屉指示器,并且由于此导航图标点击被禁用。要启用此功能,我必须将 ToolbarNavigationClickListener 添加到 ActionBarDrawerToggle,这将启用导航图标点击。

下面是我的工作代码:-

mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});

Refer this link for more clarification

关于android - 向上导航(操作栏的后退箭头)不适用于 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com