如何从活动中调用导航抽屉片段?

时间:2014-10-11 15:17:39

标签: android android-fragments navigation navigation-drawer

我有一个由Android工作室创建的导航抽屉。我从导航抽屉调用了一个活动。在该活动中,导航抽屉没有显示? 如何通过以下活动调用导航抽屉?

PlayerActivity

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

public class PlayerActivity extends Activity {

private static final int REQ_START_STANDALONE_PLAYER = 1;
private static final int REQ_RESOLVE_SERVICE_MISSING = 2;

private static final String PLAYLIST_ID = "PL5BxbbBpI7r";
public static final String DEVELOPER_KEY = "AIwXEZQLS-U";

private NavigationDrawerFragment mNavigationDrawerFragment;

private CharSequence mTitle;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
}

public void Play(View v) {
    int startIndex = 0;
    int startTimeMillis = 0;
    boolean autoplay = true;
    boolean lightboxMode = false;

    Intent intent = null;
    intent = YouTubeStandalonePlayer.createPlaylistIntent(this, DEVELOPER_KEY,
            PLAYLIST_ID, startIndex, startTimeMillis, autoplay, lightboxMode);

    if (intent != null) {
        if (canResolveIntent(intent)) {
            startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
        } else {
            // Could not resolve the intent - must need to install or update the YouTube API service.
            YouTubeInitializationResult.SERVICE_MISSING
                    .getErrorDialog(this, REQ_RESOLVE_SERVICE_MISSING).show();
        }
    }
}

private boolean canResolveIntent(Intent intent) {
    List<ResolveInfo> resolveInfo = getPackageManager().queryIntentActivities(intent, 0);
    return resolveInfo != null && !resolveInfo.isEmpty();
}

}

Navigation.java

public class Navigation extends Activity
        implements HomeFragment.OnFragmentInteractionListener, NavigationDrawerFragment.NavigationDrawerCallbacks, NewsFragment.OnFragmentInteractionListener{
    @Override
    public void onFragmentInteraction(Uri uri) {

    }

   private NavigationDrawerFragment mNavigationDrawerFragment;
   private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        mNavigationDrawerFragment = (NavigationDrawerFragment)getFragmentManager().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 = getFragmentManager();

        switch(position){
            case 0:
                HomeFragment homeFragment = new HomeFragment();
                fragmentManager.beginTransaction().replace(R.id.container, HomeFragment.newInstance("",""))
                .commit();
                break;
            case 1:
                NewsFragment newsFragment = new NewsFragment();
                fragmentManager.beginTransaction()
                .replace(R.id.container, NewsFragment.newInstance("",""))
                .commit();
                break;
            case 2:
                Intent intent= new Intent(this,PlayerActivity.class);
                startActivity(intent);
                   break;

        }

    }

    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 = getActionBar();
        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.navigation, 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);
    }

   public static class PlaceholderFragment 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 PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((Navigation) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

3 个答案:

答案 0 :(得分:1)

NavigationDrawer用于处理片段,因为抽屉本身就是一个片段。您可以访问该抽屉,只要您保持相同的Activity,但一旦您更改Activity,您就无法访问抽屉(除非您创建抽屉的另一个实例,但之后错过了它的全部意义)。为了达到你想要的目的,我推荐了两件事。

1)而不是发布新的Activity make PlayerActivity片段。

2)使用抽屉创建BaseActivity,并使所有其他活动扩展它,这样您只需对项目中的抽屉有一个引用。

答案 1 :(得分:0)

您应该使用片段而不是活动!创建项目时,默认情况下应该有两个片段。一个用于导航抽屉,另一个用于内容。您应该将该内容片段替换为您在第二个活动中放入的新内容。

答案 2 :(得分:0)

您需要在PlayerActivity

中执行相同的操作
   mNavigationDrawerFragment = (NavigationDrawerFragment)getFragmentManager().findFragmentById(R.id.navigation_drawer);
         mTitle = getTitle();
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

还将导航抽屉放在R.layout.activity_player内。

就像你在R.layout.activity_navigation

中所做的那样
相关问题