简体   繁体   English

同一导航抽屉在不同活动中具有不同的视图

[英]Same Navigation Drawer in different activities with different views

I have many activities which share the same navigation drawer. 我有许多活动共享相同的导航抽屉。 How do I write different views for all the activities with same navigation drawer. 如何为具有相同导航抽屉的所有活动编写不同的视图。 The xml and Java files can be found below. 可以在下面找到xml和Java文件。

navigation_drawer.xml 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 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 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 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 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);
}

}

I am extending NavigationDrawer in my other activities(MainActivity and Login). 我在其他活动(MainActivity和Login)中扩展了NavigationDrawer。 I want MainActivity and Login to have navigation drawer as well as their own xml views/xml files. 我希望MainActivity和Login具有导航抽屉以及它们自己的xml视图/ xml文件。 If I put some content in navigation_drawer.xml, then it would come in all activities. 如果我将某些内容放在navigation_drawer.xml中,那么它将包含在所有活动中。 I want different contents for MainActivity and Login with same navigation drawer. 我想要MainActivity和Login具有相同导航抽屉的不同内容。

Thank you. 谢谢。

Each Activity has to have his one fragment layout and inflate it from there. 每个活动都必须有自己的一个片段布局,然后从那里进行充气。 Take a look at this tutorial, it's really good: 看一下教程,它真的很棒:

Try this way to create sliding drawer,it will help 尝试这种方式创建滑动抽屉,这将有所帮助

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

See here for more detail 看到这里更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM