导航抽屉没有打开

问题描述 投票:0回答:3

在我的应用程序中,我扩展了具有导航抽屉属性的drawerActivity。问题是菜单图标出现在操作栏上。当我点击动作栏时没有任何事情发生。在这里,我发布了颂歌供您参考

drawerActivity.class

public class DrawerActivity extends ActionBarActivity {
    protected DrawerLayout mDrawerLayout = null;
    protected ListView mDrawerList = null;
    protected String[] mDrawerItems;
    protected ActionBarDrawerToggle mDrawerToggle = null;

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

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

这是userDashBoardActivity

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

            }
        });

    }
}

此活动从DrawerActivity扩展而来

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

?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="60dp"
        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="wrap_content"
        android:layout_marginLeft="350dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-60dp"
        android:padding="5dp"
        android:src="@drawable/search_icon" />


    <ImageButton
        android:id="@+id/search_btn"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="15dp"
        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="60dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="10dp"
        android:text="@string/search_college"
        android:textColor="@color/green"
        android:textSize="18sp" />

    <ImageButton
        android:id="@+id/fav_btn"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-150dp"
        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="650px"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/my_favourites"
        android:textColor="@color/green"
        android:textSize="18sp" />

    <ImageButton
        android:id="@+id/profile_btn"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/profile_blue"
        android:gravity="center" />


</LinearLayout>
android android-navigation-drawer
3个回答
0
投票

你设置的适配器是错误的你使用ArrayAdapter但你应该实际使用像这样的ActionBarDrawerToggle

private void initDrawer() {
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

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

            @Override
            public void onDrawerOpened(View drawerView) {

            }
        };
        drawerLayout.setDrawerListener(drawerToggle);
    }

如果您需要进一步的帮助,请参阅此链接qazxsw poi


0
投票

您需要在导航抽屉中添加listview,如下所示。

http://developer.android.com/training/implementing-navigation/nav-drawer.html

0
投票

在您的抽屉活动(<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 --> <android.support.design.widget.NavigationView  android:id="@+id/navigation" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" > <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.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>

DrawerActivity.class

public class DrawerActivity extends ActionBarActivity 替换actionbaractivity。我认为这应该可以解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.