当前位置: 首页 > 知识库问答 >
问题:

如何在MainActivity启动时显示“滑动菜单”?

百里涛
2023-03-14

如何在活动启动上显示“滑动菜单”?我正在使用导航抽屉显示幻灯片菜单如下代码

`public class MainActivity extends Base_Activity{private DrawerLayout MDrawerLayout;public static ListView MDrawerList;private ActionBarDrawerToggle MDrawerToggle;ImageView SelectedIcon;

Bitmap Member_Profile_Picture;
// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private TypedArray navMenuIconselected;
public static int selectedposition;
public static ArrayList<NavDrawerItem> navDrawerItems;
public static NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    selectedicon = (ImageView) findViewById(R.id.icon);
    mTitle = mDrawerTitle = getTitle();

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    //navMenuTitles=OffLine_Data.member.Get_Name();
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);
    navMenuIconselected = getResources().obtainTypedArray(
            R.array.nav_drawer_icons_selected);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    /*navDrawerItems
            .add(new NavDrawerItem(OffLine_Data.member.Get_Name(), navMenuIcons
                    .getResourceId(0, -1), navMenuIconselected
                    .getResourceId(0, -1)));*/
    //Member_Profile_Picture
    navDrawerItems
    .add(new NavDrawerItem(OffLine_Data.member.Get_Name(),navMenuIconselected
            .getResourceId(0, -1), navMenuIconselected
            .getResourceId(0, -1)));
    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
                    .getResourceId(0, -1), navMenuIconselected
                    .getResourceId(0, -1)));
    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
                    .getResourceId(1, -1), navMenuIconselected
                    .getResourceId(1, -1)));

    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
                    .getResourceId(2, -1), navMenuIconselected
                    .getResourceId(2, -1)));

    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
                    .getResourceId(3, -1), navMenuIconselected
                    .getResourceId(3, -1)));

    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
                    .getResourceId(4, -1), navMenuIconselected
                    .getResourceId(4, -1)));

    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
                    .getResourceId(5, -1), navMenuIconselected
                    .getResourceId(5, -1)));
    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[6], navMenuIcons
                    .getResourceId(6, -1), navMenuIconselected
                    .getResourceId(6, -1)));
    navDrawerItems
            .add(new NavDrawerItem(navMenuTitles[7], navMenuIcons
                    .getResourceId(7, -1), navMenuIconselected
                    .getResourceId(7, -1)));
    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    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) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

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

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

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:

        selectedposition = position;
        break;
    case 1:
        fragment = new NewsFragment();
        selectedposition = position;
        break;
    case 2:
        fragment = new FootballTeamFragment();
        selectedposition = position;
        break;
    case 3:
        fragment = new SportsActivitiesFragment();
        selectedposition = position;
        break;
    case 4:
        fragment = new RestaurantsFragment();
        selectedposition = position;
        break;
    case 5:
        fragment = new EventsFragment();
        selectedposition = position;
        break;
    case 6:
        fragment = new TalkToCoachFragment();
        selectedposition = position;
        break;
    case 7:
        fragment = new FindMyChildFragment();
        selectedposition = position;
        break;
    case 8:
        fragment = new ProfileFragment();
        selectedposition = position;
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position-1]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems, selectedposition);
    mDrawerList.setAdapter(adapter);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@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 toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}`

共有1个答案

吴哲
2023-03-14

您可以使用:(打开左边的抽屉)

  DrawerLayout.openDrawer(Gravity.LEFT);
 类似资料:
  • 我正在开发一个使用ActionBarSherlock和SlidingMenu的Android应用程序。 现在,我显示打开菜单的按钮: 但我不想表现出来 这就是我设置滑动菜单的方式: 我怎么才能去掉那个 当我点击那个按钮时,我打开了滑动菜单。 更新: 在黑带回答之后,我做了以下事情: 样式。xml: 显示xml: 结果: 我可以看到绿色的Android!!!我不想看到它!!

  • 我正在尝试用laravel动态显示菜单。 我有两个表,一个是主类,另一个是子类 下面是分类表的结构 子类别结构 这是我尝试过的 我面临的问题是,由于foreach循环,它每次都会复制主类别,例如 如您所见,每次显示子类别时,它都会复制主类别。我怎么能显示主要类别一次,然后所有子类别下面它 我有另一个模板,它有不同的菜单HTML结构 它一次又一次地生成ul和div,从而导致html错误

  • 我不知道怎么才能停止滑动菜单。我想删除可以滑动的幻灯片菜单。我试着用代码注释。但我无法停止扮演slidemenu。 下面是我的代码。你能帮助我吗?非常感谢!:)

  • 目前,我正在尝试插入一个下拉式滑动菜单(不确定它的名称)。其想法是用户触摸箭头或菜单的任何区域,然后向上拖动以打开菜单。但我不知道如何或在哪里可以找到这方面的教程。

  • 启动时,控制台显示没有错误: 自动打开的页面显示如下: 我还尝试了,显示了相同的输出。 而且很有效。

  • 问题内容: 我只希望在应用程序生命周期中仅显示一次启动屏幕。这是我的代码: SplashScreenActivity.java: 表现: 问题是如果我按硬件HOME botton隐藏该应用程序,然后在应用程序列表中再次打开该应用程序。它将再次显示启动屏幕(而不是显示)。是否只有在应用程序“新鲜”启动时才显示启动屏幕(不在onresume()上显示)?谢谢! 问题答案: 是的,有可能。使用Share