簡體   English   中英

同一導航抽屜在不同活動中具有不同的視圖

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

我有許多活動共享相同的導航抽屜。 如何為具有相同導航抽屜的所有活動編寫不同的視圖。 可以在下面找到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);
}

}

我在其他活動(MainActivity和Login)中擴展了NavigationDrawer。 我希望MainActivity和Login具有導航抽屜以及它們自己的xml視圖/ xml文件。 如果我將某些內容放在navigation_drawer.xml中,那么它將包含在所有活動中。 我想要MainActivity和Login具有相同導航抽屜的不同內容。

謝謝。

每個活動都必須有自己的一個片段布局,然后從那里進行充氣。 看一下教程,它真的很棒:

嘗試這種方式創建滑動抽屜,這將有所幫助

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

看到這里更多細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM