gpt4 book ai didi

java - 如何显示抽屉导航中的 fragment ?

转载 作者:行者123 更新时间:2023-12-01 06:09:11 24 4
gpt4 key购买 nike

我无法找到使用 Android Studio 中的特定自动生成代码的问题。我想弄清楚当从抽屉导航中单击按钮时如何显示我拥有的布局?

这里是生成的MainActivity

    package com.oxinc.android.classaverage;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
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();

//This is the part I am having trouble with

if (id == R.id.nav_camera) {
// 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) {
//This is what I have come up with
setContentView(R.layout.MyLayout)
}

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

提前谢谢您。

最佳答案

要显示一个 fragment ,您至少需要添加一个容器!下一步。让您的 android studio 默认类继承 FragmentActivity,相应地更改它们,或者您可以跳过此步骤。将 FrameLayout 放入实际放置内容的 Root View 中(android studio 默认使用抽屉为您创建了该框架)并且必须获取该 FrameLayout 的实例。创建一个类,例如 MyFragmenNumberOne.class 并让它继承 FRAGMENT 并确保使用 v4 作为旧版本的目标支持。之后您只需从fragmentManager调用patter代码来添加/删除/替换 fragment ,您将在其中传递 fragment 类和容器框架布局实例,就是这样。重要的是要提到,在调用beginTransaction.replace()时,第一个参数将不会被查看你的容器,但它的资源ID像R.id.myContainer,第二个参数是fragment的实例,它可以直接使用new关键字初始化,或者在fragment类中有一个静态newInstance()方法,该方法将创建一个实例一次在应用程序生命周期中使用它,这是更好的练习。无论如何,我帮助了你。

关于java - 如何显示抽屉导航中的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708604/

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