gpt4 book ai didi

android - 抽屉导航中每个 fragment 的不同工具栏

转载 作者:行者123 更新时间:2023-11-29 18:57:40 25 4
gpt4 key购买 nike

我有一个带有 NavigationDrawer 的 MainActivity。此 NavigationDrawer 最初与 Toolbar 同步。我想在整个应用程序中使用唯一一个 NavigationDrawer。现在的问题是每个 fragment 都有不同的工具栏或 CollapsingToolbar。

我已经读过同样的问题,但没有从这里找到答案:
Different toolbar for fragments and Navigation Drawer

在这篇文章中,一个人 Wax 给出了解决方案,但解决方案导致内存泄漏。

现在我想问一下,我怎样才能做到这一点。我尝试了很多资源,但还没有找到解决方案。

有些人正在使用 BaseActivity 方法,但我不想尝试这种方法,因为这种方法每次都需要整个 NavigationDrawer 的膨胀。

enter image description here

最佳答案

您好,只需在 main_activity.xml 的工具栏下方放置一个 frameLayout。现在,每次您需要从一个 fragment 切换到另一个 fragment 时,只需替换为 framelayout id。

按照以下步骤:

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.futuretech.animewallhd.Main2Activity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>



</android.support.design.widget.CoordinatorLayout>

ma​​in_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:openDrawer="start">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>


<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer"
/>

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

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>



</RelativeLayout>

主 Activity .java

public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

FrameLayout frameLayout;
FragmentManager fragmentManager;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

toolbar = (Toolbar) findViewById(R.id.toolbar);
frameLayout = (FrameLayout) findViewById(R.id.frame);
setSupportActionBar(toolbar);
toolbar.setTitle("Nature");
fragmentManager = getFragmentManager();
replaceFragment(new TopFrag());




DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.top) {
toolbar.setTitle("Nature");
replaceFragment(new TopFrag());

} else if (id == R.id.featured) {
toolbar.setTitle("Love");
replaceFragment(new FeatureFrag());

} else if (id == R.id.most) {
toolbar.setTitle("Featured");
replaceFragment(new MostFrag());

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

protected void replaceFragment(Fragment fragment) {
fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
}


}

fragment .java

public class DetailFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout,
container, false);
return view;
}
}

关于android - 抽屉导航中每个 fragment 的不同工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699451/

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