【发布时间】:2016-02-13 17:45:18
【问题描述】:
我尝试了很多片段事务方式,但我的默认片段在打开 navigationdrawer 活动时没有启动。
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();
}
}
它在启动时仍然首先显示活动。请传递建议,以便我可以继续前进。
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>
主类
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;
}
【问题讨论】:
-
你的方法selectDrawerItem,默认不调用。您需要更改流程才能正常工作。
-
drawer_view.xml 的内容是什么?
R.id.home里面定义了吗? -
是的,它已定义并且 xml 在菜单下。每个片段都工作正常。我的问题是,当屏幕启动时,它首先显示主要活动布局。我需要在启动时显示主要片段,而不是显示主要活动布局。@Terence
标签: java android navigation-drawer material-design