2016-04-06 8 views
8

मुझे पता है कि यह अक्सर एक प्रश्न पूछा जाता है, लेकिन स्टैक ओवरफ़्लो पर कई प्रश्न और समाधान पढ़ने के बाद मैं उलझन में हूं। मैं Fragments के संबंध में उलझन में हूं और नेविगेशन ड्रॉवर में किसी आइटम पर क्लिक करने से गतिविधि शुरू करने के लिए क्या आवश्यक है।नेविगेशन ड्रॉवर आइटम पर एक नई गतिविधि शुरू करने पर क्लिक करें

मैं इन पदों देख लिया है, लेकिन केवल Q1, उलझन में मिल गया Q2

कोई समझाने कृपया सकते हैं जो इस नेविगेशन ड्रॉर आइटम से एक मूल गतिविधि शुरू करने के लिए आवश्यक है? क्या मुझे onClick विधि लागू की गई है जहां कोड में निर्दिष्ट किया गया है? यह इरादे से कैसे संबंधित है?

यहाँ मेरी 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); 
} 

यहाँ है और दूसरा गतिविधि है, Playboard.java, कि बस एक पृष्ठभूमि छवि को लोड करता है:

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); 
    } 
} 

सभी इनपुट बहुत सराहना धन्यवाद!

उत्तर

5

प्रत्येक केस स्टेटमेंट के लिए आपको केवल Activity निर्दिष्ट करना होगा कि आप Intent के माध्यम से प्रारंभ करना चाहते हैं।

उदाहरण के लिए कहें कि navigation_item_1 चयनित होने पर आप Playboard गतिविधि शुरू करना चाहते हैं।

आप इस कोड को उस विशेष case पर जोड़ देंगे। चेतावनी के

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

का उपयोग करने के इरादे मैं विशेष रूप से किसी भी पुस्तकालय आयात करने के लिए की जरूरत है? मैंने अभी यह कोशिश की है और इरादे पर एक त्रुटि है और मैं – choloboy

+0

फ़ाइल में एक आवश्यक आयात विवरण है: 'import android.content.Intent;'। एंड्रॉइड स्टूडियो और ग्रहण में आपके लिए अनुपलब्ध आयात को हल करने में मदद करने की क्षमता है। –

+0

एक आकर्षण की तरह काम किया। बहुत बहुत धन्यवाद। – choloboy

0

एक शब्द: यदि आप drawerbox पर किसी भी एनिमेशन है, मुख्य थ्रेड से एक गतिविधि शुरू करने के लिए सीधे एनीमेशन समय से पहले ही समाप्त करने और अजीब देखने के लिए कारण होगा। इस मुद्दे को आप (कोड कमनीयता के लिए retrolambda का उपयोग करता है, लेकिन यह आवश्यक नहीं है) निम्न कर सकते हैं हल करने के लिए:

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(); 
संबंधित मुद्दे