有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在使用导航抽屉时切换片段的问题

我的导航抽屉有问题。我正在使用eclipse,并使用它为抽屉提供的代码。我已经设法添加了抽屉的附加链接。当我点击它们时,标题(在操作栏中)会正确更改,但信息不会更改。我已经在主活动中创建了所有片段。java文件。下面是我主要活动的代码。爪哇

主要活动。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类中。然而,这不起作用或使应用程序崩溃。我觉得我快要弄明白了。任何帮助都将不胜感激!谢谢


共 (1) 个答案

  1. # 1 楼答案

    这是什么代码:

    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
    }