2015-11-01 43 views
0

我有很多活動,共享相同的導航抽屜。如何爲具有相同抽屜的所有活動編寫不同的視圖。 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的不同內容並使用相同的導航抽屜登錄。

謝謝。

+0

你能發佈你的NavigationDrawer.java文件嗎 –

+0

另外,你是否使用導航畫圖呃打開不同的活動? –

+0

我在其他活動中擴展了NavigationDrawer。 – Akshay

回答

0

每個活動必須有他的一個片段佈局,並從那裏充氣​​。看看this教程,這是真正的好:

+1

雖然這是相關的前一段時間本教程已過時。我強烈建議你不要使用這個例子。 –

0

試試這個方法來創建滑動抽屜,這將有助於

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <balaji.slidingdrawer_tb.Transparent 
     android:id="@+id/popup_window" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:padding="30px"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:textSize="20dp" 
     android:paddingTop="20dp" 
     android:paddingBottom="20dp" 
      android:text="Top to Bottom Sliding" /> 

    <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="17dp" 
     android:text="Jelly Bean" /> 

    <CheckBox 
      android:id="@+id/checkBox2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="17dp" 
     android:text="Ice Cream Sandwich" /> 

    <CheckBox 
      android:id="@+id/checkBox3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="17dp" 
     android:text="HoneyComb" /> 

    </balaji.slidingdrawer_tb.Transparent> 

    <Button 
     android:id="@+id/handle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Handle"/> 

</LinearLayout> 

MainActivity.java

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    CheckBox cb1,cb2,cb3; 
    int key=0; 

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

     final Transparent popup = (Transparent) findViewById(R.id.popup_window); 
     popup.setVisibility(View.GONE); 

    final Button btn=(Button)findViewById(R.id.handle); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      if(key==0){ 
       key=1; 
       popup.setVisibility(View.VISIBLE); 
      // btn.setBackgroundResource(R.drawable.ic_launcher); 
      } 
      else if(key==1){ 
       key=0; 
       popup.setVisibility(View.GONE); 
      // btn.setBackgroundResource(R.drawable.ic_action_search); 
      } 
     } 
    }); 
    } 
} 

here更多細節