如何在oncreate活动上设置naviagationdrawer的默认片段 [英] how to set default fragment of naviagationdrawer on oncreate activity
问题描述
我尝试了很多片段事务处理方式,但是在打开NavigationDrawer活动时我的默认片段没有启动.
I tried a lot of fragment transaction ways but my default fragment is not launching while opening navigationdrawer activity.
public class MainActivity extends AppCompatActivity{
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private int mSelectedId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
toolbar.setNavigationIcon(R.drawable.nav_icon);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
navigationView = (NavigationView)findViewById(R.id.nvView);
setupDrawerContent(navigationView);
mSelectedId = R.id.home;
selectDrawerItem(mSelectedId);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem.getItemId());
return true;
}
});
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.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.
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
public void selectDrawerItem(int mSelectedId) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(mSelectedId) {
case R.id.home:
fragmentClass = Main.class;
break;
case R.id.orderhistory:
fragmentClass = Order_history.class;
break;
case R.id.profile:
fragmentClass = Profile.class;
break;
case R.id.contact:
fragmentClass = Contact.class;
break;
case R.id.polices:
fragmentClass = polices.class;
break;
default:
fragmentClass = Main.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.flContent, fragment);
fragmentTransaction.commit();
// Highlight the selected item, update the title, and close the drawer
// menuItem.setChecked(true);
// setTitle(menuItem.getTitle());
drawerLayout.closeDrawers();
}
}
它在启动时仍会首先显示活动.传递建议,以便我可以继续前进.
Its still showing the activity first while launching. do pass suggestions so as i can move forward.
Activity_main.xml
Activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context="in.co.blogspot.mellow.mellow.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id= "@+id/tool_bar"
layout= "@layout/tool_bar"></include>
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
Main.class
Main.class
public class Main extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
public Main() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,null);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
// Inflate the layout for this fragment
return rootView;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new p1();
case 1 : return new p2();
case 2 : return new p3();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
推荐答案
我认为您可以更改一些代码.
I think you can change a little your code.
以这种方式更改方法selectDrawerItem selectDrawerItem(int mSelectedId)
,以便您可以接收菜单的 id ,而不是菜单的,因此在onCreate()方法中,您可以执行以下操作
Change your method selectDrawerItem in this way selectDrawerItem(int mSelectedId)
so you can receive the id of the menu and not the menu so in onCreate() method you can do something like this
MainActivity
MainActivity
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private int mSelectedId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
navigationView = (NavigationView)findViewById(R.id.nvView);
setupDrawerContent(navigationView);
mSelectedId = R.id.orderhistory;
selectDrawerItem(mSelectedId);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem.getItemId());
return true;
}
});
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.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.
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
public void selectDrawerItem(int mSelectedId) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(mSelectedId) {
case R.id.home:
fragmentClass = Main.class;
break;
case R.id.orderhistory:
fragmentClass = Order_history.class;
break;
default:
fragmentClass = Main.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.flContent, fragment);
fragmentTransaction.commit();
// Highlight the selected item, update the title, and close the drawer
// menuItem.setChecked(true);
// setTitle(menuItem.getTitle());
drawerLayout.closeDrawers();
}
}
activity_main.xml
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
drawer_view.xml
drawer_view.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:title="Home"
/>
<item
android:id="@+id/orderhistory"
android:title="Cursos"
/>
</menu>
Order_history.java
Order_history.java
public class Order_history extends Fragment {
public Order_history() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_order_history, container, false);
}
}
fragment_order_history.xml
fragment_order_history.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.enterprise.prueba.Order_history">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Main.Java
Main.Java
public class Main extends Fragment {
public Main() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, null);
// Inflate the layout for this fragment
return rootView;
}
}
fragment_main.xml
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a fragment"/>
</LinearLayout>
已编辑
这很奇怪,现在我用相同的代码创建一个新项目,这是我在Oncreate()中选择订单历史记录时的结果.
It's very weird, now I create a new project with the same code and this is the result when I select order history in Oncreate().
我更新代码以向您显示所有文件
I Update my code to show you all the files
这篇关于如何在oncreate活动上设置naviagationdrawer的默认片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!