如何使用可重用代码以Android简单的方式创建汉堡菜单



我知道如何创建安卓汉堡菜单,有很多教程可用。

我想在每个类中创建汉堡菜单,在公共类中调用一个方法,该方法具有汉堡菜单代码。还有我的汉堡菜单在列表视图中有选项。

如何像这个汉堡菜单一样创建。我想减少我的代码并向其他人提供可重用的代码。请帮助我。

您需要获取一个活动,并在该活动的布局文件中放置以下代码:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:elevation="4dp"
    android:layout_height="fill_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            />

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_new">

<put your layout here................>
        </FrameLayout>

    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@drawable/bg_all"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:theme="@style/list_item_appearance"
        app:menu="@menu/drawer_menu" >

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

之后,在您的活动文件中:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView=(NavigationView)findViewById(R.id.navigation);
        if (drawerLayout != null) {
            drawerLayout.setDrawerShadow(R.drawable.list_back, GravityCompat.START);
            mDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this, drawerLayout,
                    toolbar, R.string.drawer_open, R.string.drawer_close) {
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    invalidateOptionsMenu();
                }
                public void onDrawerOpened(View drawerView) {
                    getSupportActionBar().setTitle(mDrawerTitle);
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    super.onDrawerOpened(drawerView);
                    invalidateOptionsMenu();
                }
            };
            drawerLayout.setDrawerListener(mDrawerToggle);
        }
 @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

然后获取一些片段,在每个布局中添加相同的导航菜单,并像这样调用它们:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);
                //Closing drawer on item click
                drawerLayout.closeDrawers();
                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {

                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.drawer_home:
                        txt_title.setText("Home");
                        Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
                        startActivity(intent);
                        overridePendingTransition(0, 0);
                        finish();
                        return true;
                    // For rest of the options we just show a toast on click
                    case R.id.drawer_artist:
                        txt_title.setText("Artists");
                        android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
                        android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.content_frame, new ArtistsFragment());
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();
                        return true;
                   }
        });

相关内容

  • 没有找到相关文章

最新更新