[英]Issue with switching fragments while using navigation drawer
即时通讯导航抽屉有问题。 我正在使用eclipse并使用它为抽屉提供的代码。 我设法将其他链接添加到抽屉中。 当我单击它们时,标题(在操作栏中)正确更改,但信息未更改。 我已经在MainActivity.java文件中创建了所有片段。 下面是我的MainActivity.java中的代码...
MainActivity.java
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().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 = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int position) {
switch (position) {
case 1:
mTitle = getString(R.string.title_suiko1);
break;
case 2:
mTitle = getString(R.string.title_suiko2);
break;
case 3:
mTitle = getString(R.string.title_suiko3);
break;
case 4:
mTitle = getString(R.string.title_suiko4);
break;
case 5:
mTitle = getString(R.string.title_suiko5);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Suikoden 1 Fragment Class
*/
public static class SuikodenFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenFragment newInstance(int sectionNumber) {
SuikodenFragment fragment = new SuikodenFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item1, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 2 Fragment Class
*/
public static class SuikodenIIFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIIFragment newInstance(int sectionNumber) {
SuikodenIIFragment fragment = new SuikodenIIFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIIFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item2, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 3 Fragment Class
*/
public static class SuikodenIIIFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIIIFragment newInstance(int sectionNumber) {
SuikodenIIIFragment fragment = new SuikodenIIIFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIIIFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item3, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 4 Fragment Class
*/
public static class SuikodenIVFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIVFragment newInstance(int sectionNumber) {
SuikodenIVFragment fragment = new SuikodenIVFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIVFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item4, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 5 Fragment Class
*/
public static class SuikodenVFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenVFragment newInstance(int sectionNumber) {
SuikodenVFragment fragment = new SuikodenVFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenVFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item5, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
我已经多次检查了该站点以提出解决方案,并发现了一些行不通的“解决方案”。 例如,将onSectionAttached内容放置在onNavigationDrawerItemSelected类内。 但是,这不起作用或使应用程序崩溃。 我觉得我快要弄清楚了。 任何帮助将非常感激! 谢谢!
这是什么代码:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
.commit();
应该是这样的:
switch(position){
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1)).commit();
//similar for others
}
我遇到了问题栏,即使用导航抽屉在Back印刷机上无法正确更新操作栏的问题,并且这些片段也无法按预期工作,因此我在此问题上发布了一个代码示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.