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

标签主机和标签在导航抽屉拉动时去

申屠锦
2023-03-14

我有一个带有滑动视图和标签导航的抽屉菜单。

我的问题是,当导航抽屉被拉出时,标签消失了,当我滑动项目时,意外停止。

下面是<code>logcat</code>

这是工作室指出错误的地方。

   mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);//error here
            }
        });

谢谢

这是活动的xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout_test"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity2Activity">


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.dot.shreyans.drwaer.MainActivity2Activity" /></FrameLayout>
<fragment android:id="@+id/navigation_drawer_test"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.dot.shreyans.drwaer.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

这是听众

mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };

共有1个答案

邓正真
2023-03-14

我没有你的完整代码,但这里有一个有效的示例

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">

<android.support.v4.view.ViewPager
    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"/>

爪哇代码

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

SectionsPagerAdapter mSectionsPagerAdapter;
PageFragment[] pageFragments;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
int page_position;

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

     mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

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

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().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) {
            getSupportActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            supportInvalidateOptionsMenu();
        }

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


    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            page_position = position;
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

    pageFragments = new PageFragment[mSectionsPagerAdapter.getCount()]; 
}

/**
 * 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
        MoveToNewActivity(position);
    }
}
 类似资料:
  • 1) 我遵循了Android开发者文档中的导航抽屉示例。Androidcom/training/implementing navigation/nav drawer。并创建了我的整个应用程序。在给定的示例中,他们为抽屉中选择的每个项目使用了片段,称为片段,代码如下 2)现在我想要一个片段内部的标签行为,即,当我在导航抽屉中选择一个特定的项目时,加载的片段应该在顶部显示一个标签栏,就像这样。http

  • 导航标签用于生成前台导航,相关的有三个标签navigation,navigationMenu,navigationFolder,三个必须联合使用 navigation标签 标签名 作用 包含属性 navigation 导航标签 nav-id,root,id,class 标签属性: | 标签属性名 | 含义 | | --- | --- | | nav-id |后台导航 id,非必须 | | root

  • 导航标签用于生成前台导航,相关的有三个标签navigation,navigationMenu,navigationFolder,三个必须联合使用 navigation标签 标签名 作用 包含属性 navigation 导航标签 nav-id,root,id,class 标签属性: | 标签属性名 | 含义 | | --- | --- | | nav-id |后台导航 id,非必须 | | root

  • 我正在为我的应用程序实现一个导航抽屉。现在,除了一个小故障外,它工作正常。当我设置导航抽屉图标(ic_Drawer)来替换常规的“HomeAsUp”插入符号图标时,我仍然得到箭头。导航抽屉图标不显示。我已经实现了android开发者网站上的每一种方法。但它似乎不起作用。 以下是我的代码:

  • 问题内容: 我有一个2活动和一个服务,首先我有导航抽屉,并且标题中有2个textview,有时我必须更改它们的文本,在服务中我需要一些数据。 第一次运行时一切都很完美,但是当我从第二个转到第一个活动,我在textviews中有nullpointerexception。是什么原因造成的,以及如何解决? 和 和 错误 问题答案: 要在Navigation Drawer的标题中成功找到任何小部件,您需要

  • 子导航标签用于生成前台导航的子导航,相关的有三个标签subNavigation,subNavigationMenu,subNavigationFolder,三个必须联合使用 subNavigation标签 标签名 作用 包含属性 subNavigation 导航标签 parent,root,id,class 标签属性: | 标签属性名 | 含义 | | --- | --- | | parent |