在现有应用程序中实现导航抽屉

时间:2014-09-10 06:33:08

标签: android android-fragments navigation-drawer

我是android新手,我正在尝试开发一个应用程序。在此应用程序中,有一个基本活动,在所有剩余的活动中都会扩展。主要问题是我想实现一个导航抽屉,我不知道如何在没有碎片的情况下实现它。任何人都可以帮我解决这个问题。

感谢名单 尼娜

2 个答案:

答案 0 :(得分:0)

您可以将某些活动转换为碎片,也可以将NavigationDrawer的逻辑放在BaseActivity中。

答案 1 :(得分:0)

检查sample code

您可以从此链接了解有关导航抽屉的更多信息:

  

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

为了在没有Fragments的情况下这样做,我在下面编写了一个代码并使用了ViewPager:

这是我的activity_main.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">

    <!-- Instead of using Fragments I used ViewPager here -->
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

这是drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp" 
    android:background="@drawable/list_selector">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/button_delete_city"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingRight="40dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"/>

    <Button
        android:id="@+id/button_delete_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="X" />

</RelativeLayout>

这是NavDrawerListAdapter:

public class NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<String> navDrawerItems;
    MainActivity act;

    public NavDrawerListAdapter(Context context, ArrayList<String> navDrawerItems, Activity act){
        this.context = context;
        this.navDrawerItems = navDrawerItems;
        this.act = (MainActivity) act;
    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {       
        return navDrawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
            Button btn_delete = (Button)convertView.findViewById(R.id.button_delete_city);
            btn_delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    act.actionDelete(position);
                }
            });

        }

        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        txtTitle.setText(navDrawerItems.get(position));
        txtTitle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                act.displayView(position);
            }
        });

        return convertView;
    }

    public void addItem(String item){
        navDrawerItems.add(item);
        notifyDataSetChanged();
    }

    public void removeItem(int position){
        navDrawerItems.remove(position);
        notifyDataSetChanged();
    }

}

这是我的MainActivity:

private ArrayList<String> navDrawerItems;
private NavDrawerListAdapter drawerAdapter;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

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

    //...
    //...other codes to implement ViewPager
    //...

    navDrawerItems = new ArrayList<String>();

    // setting the nav drawer list adapter
    drawerAdapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems, this);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList.setAdapter(drawerAdapter);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            // calling onPrepareOptionsMenu() to show action bar icons
        }

        public void onDrawerOpened(View drawerView) {
            // calling onPrepareOptionsMenu() to hide action bar icons
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }
}

public void displayView(int position) {

    mPager.setCurrentItem(position, true);

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    mDrawerLayout.closeDrawer(mDrawerList);

}