我有很多活動,共享相同的導航抽屜。如何爲具有相同抽屜的所有活動編寫不同的視圖。 xml和Java文件可以在下面找到。相同的導航抽屜在不同的活動與不同的意見
navigation_drawer.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:id="@+id/navigationLinearLayout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<ListView
android:id="@+id/navList_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|start"
android:choiceMode="singleChoice"
android:listSelector="@color/notificationarea_background"
android:background="@color/navigation_background"/>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/notificationarea_background"/>
<ListView
android:id="@+id/navList_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="@color/navigation_background"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
import android.os.Bundle;
public class MainActivity extends NavigationDrawer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.navigation_drawer, frameLayout);
}
}
Login.java
import android.os.Bundle;
public class Login extends NavigationDrawer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.navigation_drawer, frameLayout);
}
}
navigation_drawer_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/navigation_text"
android:padding="18dp"
android:layout_width="fill_parent"
android:background="@drawable/navigation_selector"
android:singleLine="true"
android:gravity="left"
android:layout_height="fill_parent"/>
NavigationDrawer.java
public class NavigationDrawer extends AppCompatActivity {
protected FrameLayout frameLayout;
public ListView mDrawerList_one, mDrawerList_two;
private ArrayAdapter<String> mAdapter_one, mAdapter_two;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
//Just the sample links.
String[] Links1 = { "Login1", "Login2", "MainActivity1" };
String[] Links2 = { "Login3", "Login4", "MainActivity2" };
protected int position_itemSelected; //Position of item selected in ListView
private static boolean isLaunch = true;
SharedPreferences itemSelected_shared;
SharedPreferences.Editor itemSelected_editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
mDrawerList_one = (ListView)findViewById(R.id.navList_one);
mDrawerList_two = (ListView)findViewById(R.id.navList_two);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
itemSelected_shared = getApplicationContext().getSharedPreferences("Item Selected in Navigation Drawer", 0);
itemSelected_editor = itemSelected_shared.edit();
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.notificationarea_background));
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
mAdapter_one = new ArrayAdapter<String>(this, R.layout.navigation_drawer_text, Links);
mDrawerList_one.setDivider(null);
mDrawerList_one.setAdapter(mAdapter_one);
if(itemSelected_shared.getInt("Item Selected", 0) == 1) {
mDrawerList_one.post(new Runnable() {
@Override
public void run() {
mDrawerList_one.setSelected(true);
mDrawerList_one.getChildAt(itemSelected_shared.getInt("Item Selected One", 0)).setBackgroundColor(getResources().getColor(R.color.notificationarea_background));
}
});
}
mAdapter_two = new ArrayAdapter<String>(this, R.layout.navigation_drawer_text, Links);
mDrawerList_two.setDivider(null);
mDrawerList_two.setAdapter(mAdapter_two);
if(itemSelected_shared.getInt("Item Selected", 0) == 2) {
mDrawerList_two.post(new Runnable() {
@Override
public void run() {
mDrawerList_two.setSelected(true);
mDrawerList_two.getChildAt(itemSelected_shared.getInt("Item Selected Two", 0)).setBackgroundColor(getResources().getColor(R.color.notificationarea_background));
}
});
}
mDrawerList_one.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
itemSelected_editor.putInt("Item Selected", 1);
itemSelected_editor.putInt("Item Selected One", position);
itemSelected_editor.commit();
}
});
mDrawerList_two.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
itemSelected_editor.putInt("Item Selected", 2);
itemSelected_editor.putInt("Item Selected Two", position);
itemSelected_editor.commit();
if (isLaunch) {
switch (position) {
case 0:
//Intent, which is working fine.
case 1:
//Intent, which is working fine.
}
}
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
我在我的其他活動對推廣NavigationDrawer
(MainActivity和登錄)。 我想MainActivity和登錄有導航抽屜以及他們自己的XML視圖/ XML文件。如果我在navigation_drawer.xml中放置了一些內容,那麼它將包含在所有活動中。我需要MainActivity的不同內容並使用相同的導航抽屜登錄。
謝謝。
你能發佈你的NavigationDrawer.java文件嗎 –
另外,你是否使用導航畫圖呃打開不同的活動? –
我在其他活動中擴展了NavigationDrawer。 – Akshay