首页 > 解决方案 > 在 xamarin android native 中滑动 tablayout 后从导航视图中选择菜单项时出现相同的视图

问题描述

我只是第一次(大约一周)使用 Xamarin android(本机),我从这里和那里获取代码以查看它是如何工作的,但我现在有点卡住了。

我有一个带有标签布局的导航视图。导航视图片段由 SupportFragmentManager 管理,稍后我将其传递给 viewpager 以将其他一些片段添加到我的 tablayout 中。

一旦我第二次从导航面板中选择一个菜单项,我的问题就会出现。例如,如果我从 MenuItem_1 切换到 MenuItem_2,MenuItem_2 的布局与我刚刚在 Menu_Item1 中看到的相同,但选项卡标题发生了变化,工具栏标题和颜色也发生了变化,我唯一的问题是选择一个 MenuItem 后剩下的将显示与第一个相同的内容。

这是我的 MainActivity 代码:

using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using MyApp.Fragments;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Content;
using TabIt
using MyApp.Fragments.Tabs;
using Android.Support.V4.App;

namespace MyApp
{
    //[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Label = "@string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
    [Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, MainLauncher = false, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon ="@drawable/icon")]
    public class MainActivity : AppCompatActivity, View.IOnClickListener
    {
        int oldPosition = -1;
        DrawerLayout drawerLayout;
        NavigationView navigationView;
        IMenuItem previousItem;        
        ImageView navMenuImg;
        TabLayout tabLayout;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.drawer_layout);

            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);

            if (toolbar != null)
            {
                SetSupportActionBar(toolbar);

                 SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                 SupportActionBar.SetHomeButtonEnabled(true);
            }

            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_black);
            navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);            
            navMenuImg = FindViewById<ImageView>(Resource.Id.nav_image);

            navigationView.NavigationItemSelected += (sender, e) =>
            {
                if (previousItem != null)
                    previousItem.SetChecked(false);

                navigationView.SetCheckedItem(e.MenuItem.ItemId);

                previousItem = e.MenuItem;

                if (toolbar != null)
                {
                    SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                    SupportActionBar.SetHomeButtonEnabled(true);                    
                }
                switch (e.MenuItem.ItemId)
                {
                    case Resource.Id.nav_home:                        
                        ListItemClicked(0);

                        break;
                    case Resource.Id.nav_item1:
                        ListItemClicked(0);
                        toolbar.Title = "MenuItem1";
                        break;
                    case Resource.Id.nav_item2:
                        ListItemClicked(1);
                        toolbar.Title = "MenuItem2";
                        break;                   
                    case Resource.Id.nav_loginItem:
                        if (toolbar != null)
                        {
                            SupportActionBar.SetDisplayHomeAsUpEnabled(false);
                            SupportActionBar.SetHomeButtonEnabled(false);
                        }
                        ListItemClicked(2);
                        break;
                }

                drawerLayout.CloseDrawers();
            };

            if (savedInstanceState == null)
            {
                navigationView.SetCheckedItem(Resource.Id.nav_MenuItem1);
                ListItemClicked(0);
            }
        }

        private void ListItemClicked(int position)
        {            
            if (position == oldPosition)
                return;

            oldPosition = position;

            Android.Support.V4.App.Fragment fragment = null;
            Android.Support.V4.App.Fragment[] fragArray = null;
            string[] titles = null;
            switch (position)
            {
                case 0:
                    fragment = Fragment1.NewInstance();  
                    break;
                    fragArray = new Android.Support.V4.App.Fragment[]
                    {
                        new TabFragment1(),
                        new TabFragment2(),
                        new TabFragment3()
                    };

                    titles = new string[]{"Item1Tab1","Item1Tab2","Item1Tab3" };

                    FnInitTabLayout(fragArray, titles);

                case 1:
                    fragment = Fragment2.NewInstance();
                    fragArray = new Android.Support.V4.App.Fragment[]
                    {
                        new TabFragment1(),
                        new TabFragment2(),
                        new TabFragment3()
                    };

                    titles = new string[]{"Item2Tab1","Item2Tab2" };

                    FnInitTabLayout(fragArray, titles);
                    break;


               case 2:
                    fragment = LoginFragment.NewInstance();
                    break;
            }

            SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.content_frame, fragment)
                .Commit();
        }



        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Android.Resource.Id.MenuItem1:
                    drawerLayout.OpenDrawer(GravityCompat.Start);
                    return true;
            }
            return base.OnOptionsItemSelected(item);
        }

        void FnInitTabLayout(Android.Support.V4.App.Fragment[] tabs, string[] fragTitles)
        {
            tabLayout.SetTabTextColors(Android.Graphics.Color.Blue, Android.Graphics.Color.White);

            var fragments = tabs;
            var titles = CharSequence.ArrayFromStringArray(fragTitles);
            var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);

            viewPager.Adapter = new TabsFragmentPagerAdapter(SupportFragmentManager, fragments, titles);

            tabLayout.SetupWithViewPager(viewPager);         

        }            
    }
}

我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/drawer_layout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <!--android:fitsSystemWindows="true"-->

<!-- The main content view -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                android:layout_alignParentTop="true"/>

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

            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                app:tabMode="fixed"
                app:tabGravity="fill" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:background="#ffffff" />
        </android.support.design.widget.AppBarLayout>        

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"
        android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>

还有我的 VievPager 适配器

using System;
using Android.Support.V4.App;
using Java.Lang;

namespace TabIt
{
    public class TabsFragmentPagerAdapter : FragmentPagerAdapter
    {
        private readonly Fragment[] fragments;

        private readonly ICharSequence[] titles;

        public TabsFragmentPagerAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
        {
            this.fragments = fragments;
            this.titles = titles;
        }
        public override int Count
        {
            get
            {
                return fragments.Length;
            }
        }

        public override Fragment GetItem(int position)
        {
            return fragments[position];
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return titles[position];
        }
    }
}

我尝试在我的主要活动中调用的几乎所有资源中使用 .Dispose() 。我还快速查看了 tabLayout 以查看它有多少个选项卡以及计数是否正确。问题只是可视化。就像我提到的那样,一旦我从导航菜单中选择了一个 MenuItem,我是否更改为另一个 MenuItem 都没有关系,我会看到相同的内容。

有人可以指出我正确的方向吗?或者至少看看是否有什么不合理的地方?

一如既往地提前感谢您。

标签: androidandroid-fragmentsmobilexamarin.androidandroid-viewpager

解决方案


推荐阅读