2016-04-06 23 views
8

Bu sık sorulan bir soru olduğunu biliyorum, ancak yığın taşmasıyla ilgili birçok soru ve çözümü okuduktan sonra kafam karıştı. Fragments ile ilgili olarak kafam karıştı ve gezinme çekmecesinde bir öğeyi tıklatmak için bir etkinlik başlatmak için gerekli olan şey.Gezinme çekmecesi üzerinde yeni bir aktivite başlatmak için tıklayın

Bunları mesajları kontrol ettik ama sadece Q1, karıştı Q2

birisi bu gezinme çekmecesi öğeden temel bir faaliyet başlatmak için nelerin gerekli olduğunu açıklayabilir misiniz? Kodda belirtilen yerlerde uygulanan onClick yöntemine ihtiyacım var mı? Ayrıca bu, Niyet ile nasıl ilgilidir?

İşte benim MainActivity.java

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

DrawerLayout drawerLayout; 
ActionBarDrawerToggle drawerToggle; 
NavigationView navigation; 

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

private void initInstances() { 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world); 
    drawerLayout.setDrawerListener(drawerToggle); 

    navigation = (NavigationView) findViewById(R.id.navigation_view); 
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      int id = menuItem.getItemId(); 
      switch (id) { 
       case R.id.navigation_item_1: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_2: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_3: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_4: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_5: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
      } 
      return false; 
     } 
    }); 

} 

@Override 
public void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    drawerToggle.onConfigurationChanged(newConfig); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (drawerToggle.onOptionsItemSelected(item)) 
     return true; 

    // 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.string.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

olduğunu Ve işte ikinci faaliyettir, Playboard.java, bu basitçe bir arka plan görüntüsü yükler:

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class Playboard extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_playboard); 
    } 
} 

Tüm giriş büyük takdir teşekkürler!

cevap

5

Her durum bildirimi için, bir Intent aracılığıyla başlamak istediğiniz Activity numaralı telefonu belirtmeniz yeterlidir.

Örneğin, navigation_item_1 seçildiğinde Playboard etkinliğini başlatmak istediğinizi varsayalım.

Bu kodu söz konusu case'a ekleyebilirsiniz. Uyarı

case R.id.navigation_item_1: 
    Intent i = new Intent(MainActivity.this, Playboard.class); 
    startActivity(i); 
    break; 
+0

kullanmak niyetini Özellikle herhangi kütüphane ithal etmek gerekiyor? Bunu şimdi denedim ve niyetle ilgili bir hatam var ve ben – choloboy

+0

Dosyada gerekli bir import ifadesi var: import android.content.Intent; '. Android Studio ve Eclipse, sizin için eksik olan ithalatı çözmenize yardımcı olabilir. –

+0

Bir çekicilik gibi çalıştım. Çok teşekkürler. – choloboy

0

Tek kelime: Eğer drawerbox üzerinde herhangi animasyonlar varsa, ana iş parçacığı bir etkinlik başlamadan doğrudan vaktinden önce sona garip bakmaya animasyon neden olacaktır. Eğer (kod prettiness için retrolambda kullanır, ancak bu gerekli değildir) aşağıdakileri yapabilirsiniz Bu soruna geçici bir çözüm için:

Class<? extends Activity> activityClass = null; 
switch (menuItem.getItemId()) { 
    case R.id.navigation_item_1: 
    activityClass = MainActivity.class; 
    break; 
} 

final Class<?> finalActivityClass = activityClass; 
Executors.newSingleThreadExecutor().execute(() -> { 
    Intent intent = new Intent(getApplicationContext(), finalActivityClass); 
    startActivity(intent); 
}); 

menuItem.setChecked(true); 
mDrawerLayout.closeDrawers(); 
İlgili konular