簡體   English   中英

使用導航抽屜時切換片段的問題

[英]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印刷機上無法正確更新操作欄的問題,並且這些片段也無法按預期工作,因此我在此問題上發布了一個代碼示例。

Android-如何在導航抽屜中更改片段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM