2016-06-21 53 views
0

我使用android studio創建了一個新的示例應用程序,並且我得到了一個帶有extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener的示例。如何使用android studio默認示例調用像片段的頁面

當我點擊抽屜菜單上的菜單項時,如何打電話給頁面(如使用片段)

這是我的代碼

package com.bertho.testapp; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.NavigationView; 
import android.support.design.widget.Snackbar; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Toast; 

import java.util.List; 

import model.Movie; 
import model.MoviesResponse; 
import rest.ApiClient; 
import rest.ApiInterface; 
import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

    private static final String TAG = MainActivity.class.getSimpleName(); 

    private final static String API_KEY = "1112223333"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      showToast("Menu Import"); 
     } else if (id == R.id.nav_gallery) { 
      showToast("Menu Galery"); 
     } else if (id == R.id.nav_slideshow) { 
      showToast("Menu Slide Show"); 
     } else if (id == R.id.nav_manage) { 
      showToast("Menu Tools"); 
     } else if (id == R.id.nav_share) { 
      showToast("Menu Share"); 
     } else if (id == R.id.nav_send) { 
      showToast("Menu Send"); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 



    public void sampleConnect() { 

     ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); 

     Call<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY); 

     call.enqueue(new Callback<MoviesResponse>() { 
      @Override 
      public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) { 
       int statusCode = response.code(); 
       List<Movie> movies = response.body().getResults(); 
       Log.d(TAG, "NUMBER OF MOVIES RECEIVED : " + movies.size()); 
      } 

      @Override 
      public void onFailure(Call<MoviesResponse> call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 
    } 


    public void showToast(String output){ 
     Toast.makeText(this.getBaseContext(), output, Toast.LENGTH_SHORT).show(); 
    } 
} 

我如何獲得我可以調用一個新的頁面,我點擊菜單上的每個菜單(onNavigationItemSelected)喜歡使用的片段?或者我應該取代

extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener

我的情況?

請幫

+0

你得到f ramelayout indside activity_main? – Sreekanth

+0

@Sreekanth我的activity_main:http://pastebin.com/Fx7S6h2E –

+0

你應該有framelayout你添加視圖使用片段 – Sreekanth

回答

1

試試這個片斷

它添加到XML抽屜佈局內

<FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

的Java

創建片段對象

Fragment fragment = new HomeFragment(); 

     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.frame_container, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
+0

你有解決方案? – Sreekanth

+0

Hai @Sreekanth對不起,回覆晚了。我又回到了網上。好的,我會嘗試將其合併到我的佈局FrameLayout中。如果我發現障礙,我會再次聯繫你 –

+0

謝謝....工作正常.. @Sreekanth –

相關問題