我使用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
我的情況?
請幫
你得到f ramelayout indside activity_main? – Sreekanth
@Sreekanth我的activity_main:http://pastebin.com/Fx7S6h2E –
你應該有framelayout你添加視圖使用片段 – Sreekanth