我的导航抽屉有问题。我正在使用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类中。但是,这不起作用或崩溃的应用程序。我觉得我已经接近搞清楚了。任何帮助将非常感激!谢谢!
答案 0 :(得分:0)
这段代码是什么:
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
}
答案 1 :(得分:0)
我遇到操作栏没有使用导航抽屉正确更新的问题,而且片段没有按预期工作,我在这里发布了一个代码示例。