Android getSupportFragmentManager导致隐藏SlideMenu导航

时间:2014-09-13 06:38:04

标签: android android-layout android-activity android-fragments

在我的应用程序中使用后:

    getSupportFragmentManager().beginTransaction().replace(R.id.drawer, getPassData(username , password )).commit();

导致隐藏SlideMenu导航项目。在表示我在左侧屏幕上有项目和导航栏之后。

ScreenShot 1 (评论前)

enter image description here

ScreenShot 2 (评论后)

enter image description here

我的布局:

activity_main.xml中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ir.tsms.Activities.DashboardActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
   </android.support.v4.view.ViewPager>

   <fragment
        android:id="@+id/navigation_drawer"
        android:name="ir.tsms.Fragments.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
         android:background="#111111"

        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

活动:

public class DashboardActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks, ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;
    public ReceivedSMS dataReceived;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getBaseContext();

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer));

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);


        ActionBar.Tab tab1 = actionBar.newTab();

        tab1.setTabListener(this);
        tab1.setIcon(R.drawable.abc_ic_voice_search);
        actionBar.addTab(tab1);


        ActionBar.Tab tab2 = actionBar.newTab();

        tab1.setIcon(R.drawable.ic_drawer);

        tab2.setTabListener(this);
        actionBar.addTab(tab2);


        ActionBar.Tab tab3 = actionBar.newTab();
        tab3.setText(R.string.title_section3);
        tab3.setTabListener(this);
        actionBar.addTab(tab3);

        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(position);
        }

    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                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()) {

            getMenuInflater().inflate(R.menu.main, menu);
//            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
       // mViewPager.setCurrentItem(tab.getPosition());
        switch ( tab.getPosition() ){
            case 0:

                Intent intent = getIntent();
                String username = intent.getStringExtra("username");
                String password = intent.getStringExtra("password");
                getSupportFragmentManager().beginTransaction().replace(R.id.drawer, getPassData(username , password )).commit();

                break;

            case 1:
                    Toast.makeText(getApplication(),"111111", Toast.LENGTH_LONG).show();
                break;
            case 2:
                    Toast.makeText(getApplication(),"222222", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";
        private int mSectionNumber = 0;

        public PlaceholderFragment() {
        }

        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;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mSectionNumber = (int) getArguments().getInt(ARG_SECTION_NUMBER);
        }

        @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);
            ((DashboardActivity) activity).onSectionAttached(getArguments().getInt(
                    ARG_SECTION_NUMBER));
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        public int getCount() {
            return 3;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为您要使用R.id.drawer中的项目替换getPassData();中的项目:

getSupportFragmentManager().beginTransaction().replace(R.id.drawer, getPassData(....)....;

所以R.id.drawer是即将到来的片段的容器。