使用继承的活动的导航抽屉实现

时间:2016-02-10 10:25:16

标签: android android-layout android-navigation-drawer

在我的应用程序中,我必须使用导航抽屉和所有活动。所以我创建了一个名为DrawerActivity的基本活动。我在DrawerActivity中编写了导航抽屉的代码,然后从UserDashBoardActivity扩展了DrawerActivity

问题是DrawerActivity属性未在UserDashBoardActivity中执行。我无法与DrawerActivity中的UserDashBoardActivity进行互动。在所有活动中,抽屉菜单位于ActionBar

这是我的DrawerActivity

public class DrawerActivity extends ActionBarActivity {

      @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.drawer_list_view);

                mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                mDrawerList = (ListView) findViewById(R.id.left_drawer);
                mDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items_array);

                //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                mDrawerList.setAdapter(new ArrayAdapter<String>(
                        this, R.layout.drawer_list_items, mDrawerItems));
                mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);

                mDrawerToggle = new ActionBarDrawerToggle(this,
                        mDrawerLayout, R.drawable.ic_menu,
                        R.string.drawer_open, R.string.drawer_close) {
                    public void onDrawerOpened(View view) {
                        invalidateOptionsMenu();
                    }

                    public void onDrawerClosed(View view) {
                        invalidateOptionsMenu();
                    }
                };
                mDrawerLayout.setDrawerListener(mDrawerToggle);
            }

            @Override
            protected void onPostCreate(Bundle savedInstanceState) {
                super.onPostCreate(savedInstanceState);
                mDrawerToggle.syncState();
            }

            @Override
            public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
                mDrawerToggle.onConfigurationChanged(newConfig);
            }

            @Override
            public boolean onPrepareOptionsMenu(Menu menu) {
                boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

                for (int index = 0; index < menu.size(); index++) {
                    MenuItem menuItem = menu.getItem(index);
                    if (menuItem != null) {
                        // hide the menu items if the drawer is open
                        menuItem.setVisible(!drawerOpen);
                    }
                }

                return super.onPrepareOptionsMenu(menu);
            }


            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }

            private class DrawerItemClickListener implements ListView.OnItemClickListener {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    switch (position) {
                        case 0: {
                            Intent intent = new Intent(DrawerActivity.this, UserDashBoardActivity.class);
                            startActivity(intent);
                            break;
                        }
                        case 1: {
                            Intent intent = new Intent(DrawerActivity.this, AdmissionActivity.class);
                            startActivity(intent);
                            break;
                        }
                        default:
                            break;
                    }
                    mDrawerLayout.closeDrawer(mDrawerList);
                }
            }

这是我的UseDashBoardActivity

public class UserDashBoardActivity extends DrawerActivity {


    private Context context;
    private ImageButton searchBtn;
    private ImageButton favBtn;
    private ImageButton profileBtn;
    private ImageButton reminderBtn;
    private ImageButton logoutBtn;
    private ImageButton notificationBtn;
    private ImageView seatchIcon;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    @Override
    protected void onStart() {
        super.onStart();
        AppActivityStatus.setActivityStarted();
        AppActivityStatus.setActivityContext(context);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppActivityStatus.setActivityStoped();

    }

    @Override
    protected void onResume() {
        super.onResume();
        AppActivityStatus.setActivityStarted();
    }

    @Override
    protected void onStop() {
        super.onStop();
        AppActivityStatus.setActivityStoped();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_user_dash_boad, menu);
        return true;
    }

    // delete the selected event from event list added here
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_notify:
                return true;

            case R.id.action_favourite:
                return true;
            case R.id.action_navigation:

        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setCustomView(R.layout.action_bar_layout);
        setContentView(R.layout.user_dash_board);
        context = getApplicationContext();
        searchBtn = (ImageButton) findViewById(R.id.search_btn);
        favBtn = (ImageButton) findViewById(R.id.fav_btn);
        profileBtn = (ImageButton) findViewById(R.id.profile_btn);
        reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
        notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
        logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
        final EditText Search = (EditText)findViewById(R.id.emailAddress);
        mDrawerList = (ListView)findViewById(R.id.drawer_layout);
        searchBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
                // Clears History of Activity
                regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(regAct);
            }
        });

        favBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0){
                Intent tabAct = new Intent(getApplicationContext(),TabHostActivity.class);
                tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(tabAct);
            }
        });

        profileBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Intent tabAct = new Intent(getApplicationContext(),AboutCollegeActivity.class);
                tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(tabAct);

            }
        });

    }
}

这是我的动作栏xml

<?xml version="1.0" encoding="utf-8"?>
<menu

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.after2.svirtzone.after2_gradle.UserDashBoardActivity">
    <item
        android:id="@+id/action_notify"
        android:icon="@drawable/mail_icon"
        appmunu:showAsAction="always"
        android:title="Notification" />
    <item
        android:id="@+id/action_favourite"
        android:icon="@drawable/favourite_icon"
        appmunu:showAsAction="always"
        android:title="Favourite" />

    <item
//this is the menu button for navigation drawer
        android:id ="@+id/action_navigation"
        android:icon="@drawable/ic_menu"
        appmunu:showAsAction = "always"
        android:title="navigation"
        android:layout_gravity="left"/>
</menu>

这是navigationdrawer listview的xml布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/activity_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" ></FrameLayout>
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

此布局为UserDashboard布局

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/appblue"
    android:orientation="vertical">

    <EditText
        android:id="@+id/emailAddress"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="@drawable/edit_text_style"
        android:gravity="center|start"
        android:hint="@string/edittext_hint"
        android:inputType="textEmailAddress"
        android:maxLength="40"
        android:textSize="18sp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/search_icon_btn"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginLeft="580dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-90dp"
        android:padding="5dp"
        android:src="@drawable/search_icon" />


    <ImageButton
        android:id="@+id/search_btn"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="210dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/search_blue"
        android:gravity="center" />

    <TextView
        android:id="@+id/searchCollege"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="20dp"
        android:text="@string/search_college"
        android:textColor="@color/green"
        android:textSize="30sp" />

    <ImageButton
        android:id="@+id/fav_btn"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="-305dp"
        android:background="@drawable/fav_blue"
        android:gravity="center" />

    <TextView
        android:id="@+id/myFavourites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="500dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:text="@string/my_favourites"
        android:textColor="@color/green"
        android:textSize="30sp" />

</LinearLayout>

UserDashboard活动按原样执行,但我需要DrawerActivity的属性与此活动一起执行。怎么做?

2 个答案:

答案 0 :(得分:0)

让家长Activity添加所需的ViewGroup作为FrameLayout的子级。要实现此目的,请使用以下方法进行DrawerActivity:

protected void addToContentView(int layoutResID)
{
    // as part of onCreate() we had already
    // setContentView(R.layout.activity_drawer);

    if (layoutResID >= 0)
    {
        Toast.makeText(this, "activity content found", Toast.LENGTH_LONG).show();
        FrameLayout fl = (FrameLayout)findViewById(R.id.activity_frame);
        View.inflate(this, layoutResID, fl);
    }
    else
    {
        Toast.makeText(this, "activity content missing", Toast.LENGTH_LONG).show();
    }
}

然后,在孩子的onCreate()方法中Activity

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // call this instead of setContentView() 
    addToContentView(R.layout.activity_user_dashboard);

    // put here your code as before...
}

答案 1 :(得分:0)

抽屉的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            />

		<!--you need to add everything to this frameLayout then only you will be able to access Navigation DrawerLayout-->
        <FrameLayout
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/navigation_list"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        />


</android.support.v4.widget.DrawerLayout>

带抽屉的活动:(仅显示重要代码)

public abstract class BaseDrawerLayoutActivity extends AppCompatActivity implements OnItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_drawer_layout_activity);
    //rest of the things
    //NOTE: loadNavMenu is abstract so that you can implement it in other activities as per you need, otherwise remove this and load it here itself
    loadNavMenu();
}

protected abstract void loadNavMenu();

// call this method whenever you enter new activity and load the fragment here. 
public void showFragment(Fragment object, String title) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, object);
    transaction.commit();
}}

需要抽屉菜单的那些活动可以继承上面的类。不仅仅在FrameLayout内部扩展所有布局。

public class MainActivity extends BaseDrawerLayoutActivity {
public static final String TAG = OLMSActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //the method below will call the BaseDrawerLayoutActivity method and change the fragment for you to see.
    showFragment(new MyProfileFragment(), "My Profile");
}

@Override
protected void loadNavMenu() {
    NavItems.clear();
    //load your menu items
}

private void loadNavFavourites(){
    //todo
}}

MyProfileFragment只是一个带有片段的类,这一个调用会在其OnCreateView方法中扩展一个布局。