如何使用Android导航抽屉来更改片段
我知道这些types的问题已经在这里,但我仍然没有find我对这个问题的答案:
- 我创build了一个应用程序,并使用由应用程序(AndroidStudio)创build的AUTOMATICLLY的导航抽屉,
这是我有:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); // Set up the drawer. mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); } public void onSectionAttached(int number) { switch (number) { case 1: break; case 2: break; case 3: break; } }
还有一些在这里:
public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).onSectionAttached( getArguments().getInt(ARG_SECTION_NUMBER)); } }
我想要使用导航抽屉中的button显示另一个片段。 我想使用这个代码,所以请不要给我任何指导或教程制作自己的抽屉。
问题是, case 1:
case 2:
和case 3:
如果我想打开另一个片段? 感谢名单。
还有一个问题:
-
我如何添加更多的碎片和交易? 这不起作用 –
Fragment fragment = new MyFragment1(); Fragment frag = new MyFragment2(); FragmentManager fragmentManager = getFragmentManager(); switch(position) { case 0: fragment = new MyFragment1(); break; case 1: frag = new MyFragment2(); break; } fragmentManager.beginTransaction() .replace(R.id.container, fragment).commit();
}
你应该把一个switch语句放到onNavigationDrawerItemSelected
方法中。
像这样的东西应该工作:
public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments Fragment fragment; FragmentManager fragmentManager = getFragmentManager(); // For AppCompat use getSupportFragmentManager switch(position) { default: case 0: fragment = new MyFragment1(); break; case 1: fragment = new MyFragment2(); break; } fragmentManager.beginTransaction() .replace(R.id.container, fragment) .commit(); }
这只是快速完成,但我认为它应该工作
我通过inflater
解决了这个问题:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView; switch(getArguments().getInt(ARG_SECTION_NUMBER)) { case 1: rootView = inflater.inflate(R.layout.fragment_obj_detail, container, false); break; case 2: rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 3: rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 4: rootView = inflater.inflate(R.layout.fragment_about, container, false); break; default: rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); } return rootView; }
您需要在onNavigationDrawerItemSelected(
)中创build一个switch
块,并使用已经存在的代码来处理每种case
但使用对应的Fragment
而不是PlaceholderFragment
。 现在它包含一个通用的代码片段,用于将PlaceholderFragment
添加到布局,并将其重用于您的目的。
DenisGl的回应是正确的! 使用默认创build的类成员,您可以在导航抽屉的各个组件之间切换! 您必须使用类成员PlaceholderFragment中的onCreateView方法。 这个类将在方法onNavigationDrawerItemSelected中被自动调用
这里是代码示例:/ 这个方法可以保持原样! 它会自动调用类PlaceholderFragment! /
@Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); }
这种方法,而是在你进入开关柜的地方 :
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = null; rootView = inflater.inflate(R.layout.fragment_home, container, false); switch(getArguments().getInt(ARG_SECTION_NUMBER)) { case 1: rootView = inflater.inflate(R.layout.fragment_home, container, false); break; case 2: //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 3: //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 4: rootView = inflater.inflate(R.layout.fragment_info, container, false); break; } return rootView; }
显然,你必须调用每个我们感兴趣的片段的布局!
这工作100%,testingvalidation!
它在Eclipse IDE中工作
switch(getArguments().getInt(ARG_SECTION_NUMBER)) { case 1: rootView = inflater.inflate(R.layout.fragment_home, container, false); break; case 2: //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 3: //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false); break; case 4: rootView = inflater.inflate(R.layout.fragment_info, container, false); break; }
function
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = null; rootView = inflater.inflate(R.layout.fragment_home, container, false); return rootView; }