使用Android ActionBar在活动之间导航

时间:2013-05-29 19:07:01

标签: android android-activity android-actionbar xamarin.android

我正在尝试重用Android ActionBar中的NavigationBar来在屏幕之间导航。由于每个屏幕都包含许多单独的逻辑,我希望每个屏幕都有一个Activity,但每个屏幕应该重用ActionBar,因为它包含导航和其他自定义视图。

为了做到这一点,我创建了一个基本活动类,用于生成相应的操作栏。以下代码按预期工作,但有些事情感觉不太对劲。看起来我似乎做了太多工作,应该更简单。此外,即使我的基本活动重用了大量代码,它实际上并没有在内存中重用ActionBar。

我可以使用不同的原生导航控件来实现此目的吗?我不想做自定义布局膨胀或创建自定义导航栏。

另外,我正在使用Xamarin的Mono for Android。

基本导航活动

public abstract class BaseNavigationActivity : Activity
{
    private NavigationScreens[] _availableScreens = { NavigationScreens.Screen1, NavigationScreens.Screen2 };
    private NavigationScreens _currentScreen = NavigationScreens.Unknown;

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

        // Remove ActionBar icon and title. Also allow custom view.
        ActionBar.SetDisplayShowHomeEnabled(false);
        ActionBar.SetDisplayShowTitleEnabled(false);

        // Navigation bar
        ScreenNavigationAdapter screenAdapter = new ScreenNavigationAdapter(this, _availableScreens);
        ScreenNavigationListener navigationListener = new ScreenNavigationListener(this, screenAdapter);

        ActionBar.NavigationMode = ActionBarNavigationMode.List;
        ActionBar.SetListNavigationCallbacks(screenAdapter, navigationListener);
    }

    protected override void OnResume()
    {
        base.OnResume();

        // Set the navigation item on the action bar to be the current screen
        ActionBar.SetSelectedNavigationItem(Array.FindIndex(_availableScreens, x => x == _currentScreen));
    }

    public NavigationScreens CurrentScreen
    {
        get { return _currentScreen; }
    }

    public BaseNavigationActivity(NavigationScreens screen)
    {
        _currentScreen = screen;
    }

示例活动

[Activity(Label = "ActionBarTest", MainLauncher = true, Theme = "@style/CustomActionBarStyle", LaunchMode = LaunchMode.SingleInstance)]
public class ActionBarNavigationTestActivity : BaseNavigationActivity
{
    public ActionBarNavigationTestActivity()
        : base(NavigationScreens.Screen1)
    {
    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.ActionBarNavigation);
    }
}

适配器和听众

public class ScreenNavigationAdapter : BaseAdapter
{
    private IEnumerable<NavigationScreens> _screens;
    private LayoutInflater _layoutInflater;

    public ScreenNavigationAdapter(Context context, IEnumerable<NavigationScreens> screens)
    {
        _screens = screens;

        // Retrieve the layout inflater from the provided context
        _layoutInflater = LayoutInflater.FromContext(context);
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView;

        // Try to reuse views as much as possible.
        if (view == null)
        {
            // inflate a new layout for the view.
            view = _layoutInflater.Inflate(Resource.Layout.SpinnerViewLayout, parent, false);
        }

        TextView textView = view.FindViewById<TextView>(Resource.Id.MenuText);
        textView.Text = _screens.ElementAt(position).ToString();

        return view;
    }

    public override int Count
    {
        get { return _screens.Count(); }
    }

    public NavigationScreens GetScreenAt(int position)
    {
        return _screens.ElementAt(position);
    }
}

public class ScreenNavigationListener : Java.Lang.Object, ActionBar.IOnNavigationListener
{
    private readonly BaseNavigationActivity _currentActivity = null;
    private readonly ScreenNavigationAdapter _adapter = null;

    public ScreenNavigationListener(BaseNavigationActivity currentScreen, ScreenNavigationAdapter adapter)
    {
        _currentActivity = currentScreen;
        _adapter = adapter;
    }

    public bool OnNavigationItemSelected(int itemPosition, long itemId)
    {
        NavigationScreens selectedScreen = _adapter.GetScreenAt(itemPosition);
        if (_currentActivity.CurrentScreen == selectedScreen)
        {
            // Already displaying the selected screen, dont do anything
            return true;
        }

        Intent intent = null;
        if (selectedScreen == NavigationScreens.Screen1)
        {
            intent = new Intent(_currentActivity, typeof(ActionBarNavigationTestActivity));
        }

        if (selectedScreen == NavigationScreens.Screen2)
        {
            intent = new Intent(_currentActivity, typeof(ActionBarNavigationTestActivity2));
        }

        if (intent != null)
        {
            intent.SetFlags(ActivityFlags.NoAnimation);
            _currentActivity.StartActivity(intent);

            return true;
        }

        return false;
    }
}

public enum NavigationScreens
{
    Unknown = -1,
    Screen1 = 0,
    Screen2
}

0 个答案:

没有答案