5

मैं एंड्रॉइड स्टूडियो के नेविगेशन ड्रॉवर टेम्पलेट का उपयोग कर एक एप्लिकेशन विकसित करने की कोशिश कर रहा हूं। इसलिए, मैंने इस टेम्पलेट का उपयोग करके एक नई परियोजना बनाई है। लेकिन जब मैं प्रोग्राम चलाता हूं और मेनू आइटम पर क्लिक करता हूं, तो दृश्य नहीं बदलता है। मैंने इंटरनेट पर हर जगह खोज की, लेकिन मैंने नहीं देखा कि मैं इसे कैसे संभाल सकता हूं।खंड या गतिविधियों के साथ एंड्रॉइड स्टूडियो नेविगेशन ड्रॉवर

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 

    int id = item.getItemId(); 

    if (id == R.id.nav_camara) { 


    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

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

क्या मैं हासिल करना चाहते हैं पर क्लिक किया मेनू आइटम के लिए उचित दृष्टि से वर्तमान दृश्य को बदलने के लिए है:

यह एंड्रॉयड स्टूडियो द्वारा प्रदान की कोड है।

क्या फ्रैगमेंट ऐसा करने का सबसे अच्छा तरीका है या क्या मुझे प्रत्येक मेनू आइटम के लिए एक अलग गतिविधि बनाना चाहिए?

+0

आप टुकड़ा का उपयोग करना चाहिए क्योंकि आसान उपयोग एक ही नेविगेशन एक गतिविधि के अंदर दराज। – GiapLee

उत्तर

17

आपके मामले में टुकड़ा जोड़ने का सबसे अच्छा समाधान होगा।

एक टुकड़ा BlankFragment.java

public class BlankFragment extends Fragment { 

    public BlankFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_blank, container, false); 
    } 

} 

बना सकते हैं और बनाने fragment_black.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.above_inc.shyam.drawer.BlankFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

अब की जगह अपने विधि

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

    Fragment fragment = null; 
    if (id == R.id.nav_camera) { 
     // Handle the camera action 
     fragment = new BlankFragment(); 
    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

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

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

} 

अपने content_main.xml में नीचे दिए गए कोड को जोड़ने

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.above_inc.shyam.drawer.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 


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

आप भी उपरोक्त कोड में कैमरे के रूप में एक ही अन्य विकल्पों के लिए अधिक टुकड़ा जोड़ सकते हैं

+0

यह समाधान @DHAKAD काम करता है। – Royken

2

यदि आप मेनू आइटम पर क्लिक करते हैं तो अगली स्क्रीन पर अपना नेविगेशन ड्रॉवर दिखाना चाहते हैं तो फ्रैगमेंट सबसे अच्छा तरीका होगा।

यदि आप गतिविधि का उपयोग करेंगे तो नेविगेशन ड्रॉवर तब तक नहीं दिखाया जाएगा जब तक आप बेसएक्टिविटी नहीं बनाते जो नेविगेशन ड्रॉवर को बढ़ाता है और हर जगह उपयोग करता है। इस मामले में आपको एसिटिवटी संक्रमण एनीमेशन भी बदलना होगा क्योंकि मेनू पर क्लिक करने के बाद नई गतिविधि पॉप अप हो जाएगी जो अजीब लग सकती है।

0

अपनी परियोजना -> नया -> टुकड़ा पर राइट क्लिक करें।

तदनुसार स्विच केस संपादित करें।

0

@Passiondroid'sanswer केवल फ्रैगमेंट्स का उपयोग करने पर, यदि आप चाहते हैं कि सभी गतिविधियों में नेविगेशन ड्रॉवर चालू हो।

मेरी क्षुधा में से एक में, मेनू आइटम के क्लिक पर एक गतिविधि शुरू करने के बारे में, मैं इस का पालन करें,

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

     switch(id) 
     { 

      case R.id.nav_profile: 
      selection = 1; 
       intent = new Intent("android.intent.action.WEB"); 
       intent.putExtra("selection",selection); 
       startActivity(intent); 
       break; 
      case R.id.nav_books: 
       intent = new Intent("android.intent.action.WEBD"); 
       selection = 2; 
       intent.putExtra("selection",selection); 
       startActivity(intent); 
       break; 
      case R.id.fav_quotes: 
       intent = new Intent ("android.intent.action.SHOWFAVTES"); 
       selection = 3; 
       intent.putExtra("selection",selection); 
       startActivity(intent); 
       break; 
      default: 
       break; 
     } 

बदल सकते हैं और मेनू आइटम कॉन्फ़िगर करने के लिए, आप "activity_main_drawer.xml" के तहत प्राप्त कर सकते हैं res-> मेनू।

और मेनू मेनू में से प्रत्येक के क्लिक पर, आप अपनी इच्छा के अनुसार एक गतिविधि शुरू कर सकते हैं। आपने इसका उपयोग करने से पहले अपने AndroidManifest.xml फ़ाइल में गतिविधियों की घोषणा की है।

संबंधित मुद्दे