2014-10-11 110 views
0

我有一个由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)); 
     } 
    } 

} 

回答

0

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

+0

我还没有创建过这个项目。 Android工作室本身从模板创建此导航抽屉项目。 – AruLNadhaN 2014-10-11 15:48:29

+0

我无法将播放器活动更改为片段,因为它使用Youtube API。 – AruLNadhaN 2014-10-11 16:01:18

+0

使用Youtube API的YouTubePlayerFragment https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerFragment – iamkaan 2014-10-11 16:17:33

1

NavigationDrawer是为了处理片段,因为抽屉本身就是片段。只要您保持在同一个Activity中,您可以访问该抽屉,但是一旦您更改Activity,您将无法访问抽屉(除非您创建抽屉的另一个实例,但是然后您错过了它的全部要点)。为了完成你想要的东西,我推荐两件事。

1)而不是启动一个新的Activity使PlayerActivity一个片段。

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

+0

我无法将播放器活动更改为片段,因为它是使用Youtube API。 – AruLNadhaN 2014-10-11 16:04:45

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所做的那样