gpt4 book ai didi

android - 在新的 Navigation Drawer Activity 模板中使用 onNavigationItemSelected 在 Fragments 之间切换(Android Studio 1.4 及更高版本)

转载 作者:IT王子 更新时间:2023-10-29 00:01:09 53 4
gpt4 key购买 nike

IntelliJ 对 Android Studio 中的 Navigation Drawer 模板 Activity 进行了更改,减少了 Activity 类中的代码行。新的 Activity 类如下所示:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

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.setDrawerListener(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();
}
}


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

if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

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

这里最显着的变化之一是方法:

onNavigationItemSelected(MenuItem item)

旧的 Navigation Drawer 模板对此方法的定义是:

onNavigationItemSelected(int position, long itemId)

您可以通过删除内部 PlaceHolderFragment 类来修改旧模板,创建自己的 fragment 和布局并执行以下操作:

Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

}

但这不适用于新模板(至少据我所知不是)。我试过了:

 public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), item.getTitle() + " clicked", Snackbar.LENGTH_SHORT);
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = HomeFragment.getFragInstance();
break;

case R.id.nav_news:

fragment = NewsFragment.getFragInstance();
break;


default:
break;
}

if (fragment != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.drawer_layout, fragment);
transaction.commit();


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

}
return true;
}

home 布局的布局也显示在 news 布局中。这可能是因为以下行:

transaction.replace(R.id.drawer_layout, fragment);

fragment 应该在 FrameLayout 中被替换,旧的 Navigation Drawer 布局看起来像这样:

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

<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- Listview to display slider menu -->


<ListView
android:id="@+id/list_sliderMenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/white"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>

但新的看起来像这样:

 <?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">

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

<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_base"
app:menu="@menu/activity_base_drawer" />

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

长话短说,如何修改新模板才能在 fragment 之间切换?

最佳答案

所以,根据@L.L. 的回答,我能够解决这个问题。

首先,将你的 FrameLayout 添加到你的 content_main.xml 文件中:

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

在你的 MainActivity(或任何你用抽屉导航命名的 Activity )定义一个名为 displayView

的方法
 public void displayView(int viewId) {

Fragment fragment = null;
String title = getString(R.string.app_name);

switch (viewId) {
case R.id.nav_news:
fragment = new NewsFragment();
title = "News";

break;
case R.id.nav_events:
fragment = new EventsFragment();
title = "Events";
break;

}

if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}

// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}

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

}

在 3 个自定义 fragment 之间切换; NewsFragment、EventsFragment 和 GalleryFragment。

在我的菜单activity_main_drawer我已经把内容改成了这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
<item android:id="@+id/nav_news"
android:icon="@android:drawable/ic_menu_news"
android:title="News" />
<item android:id="@+id/nav_events"
android:icon="@android:drawable/ic_menu_events"
android:title="Events" />
<item android:id="@+id/nav_gallery"
android:icon="@android:drawable/ic_menu_gallery"
android:title="Gallery" />
</group>
</menu>

回到 Activity 类,在您的 onNavigationItemSelected 方法中这样做:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
displayView(item.getItemId());
return true;
}

最后,您的 onCreate 方法中的最后一条语句:

 @Override
protected void onCreate(Bundle savedInstanceState) {
....
....
displayView(R.id.nav_news);
}

这是因为我希望我的用户看到的第一个 View 是新闻。将其更改为您选择的任何内容。

处理后按事件:

就目前而言,如果您按下任何 fragment 的后退按钮,应用程序将退出。我希望我的应用程序在用户按下后退按钮时返回新闻 fragment (我的主页 fragment )。所以我这样做了:

声明一个 bool 变量:

private boolean viewIsAtHome;

然后在 displayView() 方法中我这样做了:

 public void displayView(int viewId){
Fragment fragment = null;
String title = getString(R.string.app_name);

switch (viewId) {
case R.id.nav_news:
fragment = new NewsFragment();
title = getString(R.string.news_title);
viewIsAtHome = true;

break;
case R.id.nav_events:
fragment = new EventsFragment();
title = getString(R.string.events_title);
viewIsAtHome = false;
break;

case R.id.nav_gallery:
fragment = new GalleryFragment();
title = getString(R.string.gallery_title);
viewIsAtHome = false;
break;

最后,删除旧的 onBackPressed 方法,并在 onCreate() 方法之外创建一个类似 的新方法:

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
if (!viewIsAtHome) { //if the current view is not the News fragment
displayView(R.id.nav_news); //display the News fragment
} else {
moveTaskToBack(true); //If view is in News fragment, exit application
}
}

为我工作。

关于android - 在新的 Navigation Drawer Activity 模板中使用 onNavigationItemSelected 在 Fragments 之间切换(Android Studio 1.4 及更高版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32944798/

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