धन्यवाद (आप अंतर-टुकड़ा संचार में इंटरफेस बारे में सीखना होगा) मैं चाहता था, यहाँ है कि वास्तव में क्या मैं भविष्य में इस की तलाश में किसी के लिए भी किया था:
मेरे activity_main.xml इस तरह दिखता है:
<!--When the DrawerLayout is the root layout, the first child-->
<!--of that layout is the contents of the main screen, and the-->
<!--second child is the contents of the menu-->
<!--First child layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
</LinearLayout>
<!--Second child layout-->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
यह मानक ड्रॉवरलाउट है जो नेविगेशन ड्रावर मेनू के लिए सभी बिट्स और टुकड़ों को एक साथ खींचता है। इसके लिए महत्वपूर्ण जोड़ फ्रेमलाउट है ... बिट जिस पर मैंने आईडी content_frame दिया था। यह वह जगह है जहां अन्य गतिविधियों द्वारा उपयोग किए जाने वाले सभी अन्य लेआउट को धक्का/जोड़ा/फुलाया जाएगा।
मेरे BaseActivity.java इस तरह दिखता है:
package com.example.test;
import android.content.Intent;
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.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
Intent anIntent = new Intent(getApplicationContext(), TheClassYouWantToLoad.class);
startActivity(loadPlayer);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
अब, onNavigationItemSelected विधि के भीतर, वहाँ एक स्विच बयान है कि संभालती है क्या होता है जब प्रत्येक मेनू आइटम का चयन किया गया है। यह महत्वपूर्ण बात है:
Intent anIntent = new Intent(getApplicationContext(), TheClassYouWantToLoad.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
आपको अपनी कक्षा के साथ "TheClassYouWantToLoad" को प्रतिस्थापित करने की आवश्यकता है।
public class TheClassYouWantToLoad extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your activity_main.xml
getLayoutInflater().inflate(R.layout.the_layout_you_want_to_load, contentFrameLayout);
}
}
और लेआउट आप लोड करना चाहते हैं के नाम के साथ "the_layout_you_want_to_load" की जगह: अब के भीतर इस नए वर्ग (जो संभवतः कुछ नए UI तत्व की आवश्यकता है लोड करने के लिए), आपको निम्न की जरूरत है।
वैसे, मैं अब क्या कर रहा हूं कि मेरे पास बेसएक्टिविटी है जो नेविगेशन ड्रावर को जगह में रखती है और मुख्य लेआउट लोड करती है। मुझे पता है कि मैं इस कोड को हर एक गतिविधि में दोहरा सकता हूं, लेकिन मैं कोड दोहराने से बचने की कोशिश कर रहा हूं। मैंने अपनी अन्य कक्षाओं को इस बेसएक्टिविटी का विस्तार करने की कोशिश की, लेकिन तब मुझे नहीं पता कि मेनू –