导航抽屉菜单Android - 项目列表视图组

时间:2015-02-18 23:11:04

标签: android android-listview navigation-drawer

我正在尝试在android中进行导航器抽屉菜单,直到现在我做到了: enter image description here

但是我想要一组像涂上这个编辑过的图像的项目: enter image description here

我该怎么办? 如何分割一些物品?

谢谢!

编辑:

navigation_drawer_base_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

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

<ListView
    android:id="@+id/nav_list"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    android:choiceMode="singleChoice" />

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

BaseActivity.java

package br.com.tokendev.mymoney;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

/**
 * This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
 * We are going to extend all our other activites from this BaseActivity so that every activity will have Navigation Drawer in it.
 * This activity layout contain one frame layout in which we will add our child activity layout.   
 */
public class BaseActivity extends ActionBarActivity {

/**
 *  Frame layout: Which is going to be used as parent layout for child activity layout.
 *  This layout is protected so that child activity can access this 
 *  */
protected FrameLayout frameLayout;

/**
 * ListView to add navigation drawer item in it.
 * We have made it protected to access it in child class. We will just use it in child class to make item selected according to activity opened. 
 */

protected ListView mDrawerList;

/**
 * List item array for navigation drawer items.
 * */
protected String[] listArray;
protected String[] listIconArray;

/**
 * Static variable for selected item position. Which can be used in child activity to know which item is selected from the list. 
 * */
protected static int position;

/**
 *  Base layout node of this Activity.   
 * */
protected DrawerLayout mDrawerLayout;

/**
 * Drawer listner class for drawer open, close etc.
 */
private ActionBarDrawerToggle actionBarDrawerToggle;

private Context mContext;


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

    mContext = this;

    frameLayout = (FrameLayout)findViewById(R.id.content_frame);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.nav_list);

    listArray = getResources().getStringArray(R.array.menu_list);
    listIconArray = getResources().getStringArray(R.array.menu_list_icon);
    ArrayList<NavDrawerMenu> listMenu = new ArrayList<NavDrawerMenu>();
    for (int i=0; i<listArray.length; i++) {
        listMenu.add(new NavDrawerMenu(listArray[i],getResources().getIdentifier(listIconArray[i], "drawable", this.getPackageName())));
    }

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    // set up the drawer's list view with items and click listener
    ListViewDrawerMenu adapter = new ListViewDrawerMenu(mContext,
            R.layout.navigation_drawer_base_layout, listMenu);
    mDrawerList.setAdapter(adapter);

    //mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listArray));
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            openActivity(position);
        }
    });

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions between the sliding drawer and the action bar app icon
    actionBarDrawerToggle = new ActionBarDrawerToggle(
            this,                       /* host Activity */
            mDrawerLayout,              /* DrawerLayout object */
            R.string.app_name,       /* "open drawer" description for accessibility */
            R.string.app_name)      /* "close drawer" description for accessibility */
    {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            super.onDrawerStateChanged(newState);
        }
    };

    actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
}

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

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

/**
 * @param position
 *
 * Launching activity when any list item is clicked.
 */
protected void openActivity(int position) {

    /**
     * We can set title & itemChecked here but as this BaseActivity is parent for other activity,
     * So whenever any activity is going to launch this BaseActivity is also going to be called and
     * it will reset this value because of initialization in onCreate method.
     * So that we are setting this in child activity.   
     */

    mDrawerLayout.closeDrawer(mDrawerList);
    if (position == BaseActivity.position) return;

    if (BaseActivity.position != 0) finish();

    BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/* We can override onBackPressed method to toggle navigation drawer*/
@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        BaseActivity.position = 0;
        super.onBackPressed();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用三个列表视图或仅在包含分隔符的位置包含null值的一个列表来执行此操作:

  1. 使用两种布局:一种用于项目,另一种用于分隔符。

  2. 像往常一样在构造函数中创建一个带有项列表作为参数的适配器。

  3. 在您要传递的列表中,将要作为分隔符的项目设置为null

  4. 在适配器中,在getView上实施,检查当前项目是否为null,以创建分隔符,以创建常规项目。

    < / LI>