使用ActionBar Tab(地址)

本文实现将页面分为多个选项卡,并在每一个选项卡中显示一个ListView。

创建新Layout - ActionbarTab.axml, 并向页面中添加FrameLayout控件。 页面源代码如下:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<FrameLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frameLayout1" />
</LinearLayout>

创建新Layout - ListViewLayout.axml, 这个Layout用于定义嵌在Frame - frameLayout1中的ListView控件。 在页面中添加ListView控件 - Id是myList。页面源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myList" />
</LinearLayout>

向项目中添加新Activity - ActionbarTabActivity.cs, 在Activity中实现新Tab的创建。

  1. Oncreate()方法中,关联前端UI.

    SetContentView(Resource.Layout.ActionbarTab);
  2. 设置页面导航模式为Tabs.

    this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  3. 定义新AddTab方法, 将新创建的Fragment View添加到页面控件frameLayout1中。

    void AddTab(string tabText, int iconResourceId, Fragment view)
    {
    var tab = this.ActionBar.NewTab();
    tab.SetText(tabText);
    tab.SetIcon(iconResourceId); tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
    {
    var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
    if (fragment != null)
    e.FragmentTransaction.Remove(fragment);
    e.FragmentTransaction.Add(Resource.Id.frameLayout1, view);
    };
    tab.TabUnselected += delegate (object sender, ActionBar.TabEventArgs e)
    {
    e.FragmentTransaction.Remove(view);
    }; this.ActionBar.AddTab(tab);
    }
  4. 定义两个新Fragment类(此处仅demo一个),继承基类Fragment。Fragment类似于子Activity, 这两个Fragment的作用是操作两个Tab页面中的控件。

    public class firstFragment:Fragment
    {
    List<string> Sources = null; public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    base.OnCreateView(inflater, container, savedInstanceState);
    var view = inflater.Inflate(Resource.Layout.ListViewlayout, container, false);
    ListView myList = view.FindViewById<ListView>(Resource.Id.myList); Sources = new List<string>();
    for(int i = 1; i<=10;i++)
    {
    Sources.Add(string.Format(@"item {0}", i));
    }
    ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
    global::Android.Resource.Layout.SimpleListItem1, Sources);
    myList.SetAdapter(adapter);
    myList.ItemClick += ListClick;
    } void ListClick(object sender, AdapterView.ItemClickEventArgs e)
    {
    ListView list = sender as ListView;
    string selectedFromList = list.GetItemAtPosition(e.Position).ToString();
    }
    }

    以上代码中需要注意, ArrayAdapter的第一参数在Fragment中是this.Activity, 而在Activity中是thisFindViewById()修改为view.FindViewById()

  5. 添加新Tab.

    AddTab("list1", Resource.Drawable.Icon, new firstFragment());

ActionbarTabActivity 源代码如下:

[Activity(Label = "ActionbarTabActivity")]
public class ActionbarTabActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Create your application here
SetContentView(Resource.Layout.ActionbarTab);
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; AddTab("list1", Resource.Drawable.Icon, new firstFragment());
if(bundle!=null)
this.ActionBar.SelectTab(this.ActionBar.GetTabAt(bundle.GetInt("tab")));
}
protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
base.OnSaveInstanceState(outState);
}
void AddTab(string tabText, int iconResourceId, Fragment view)
{
var tab = this.ActionBar.NewTab();
tab.SetText(tabText);
tab.SetIcon(iconResourceId); tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
e.FragmentTransaction.Add(Resource.Id.frameLayout1, view);
};
tab.TabUnselected += delegate (object sender, ActionBar.TabEventArgs e)
{
e.FragmentTransaction.Remove(view);
}; this.ActionBar.AddTab(tab); } public class firstFragment:Fragment
{
List<string> Sources = null; public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.ListViewlayout, container, false);
ListView myList = view.FindViewById<ListView>(Resource.Id.myList); Sources = new List<string>();
for(int i = 1; i<=10;i++)
{
Sources.Add(string.Format(@"item {0}", i));
}
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
global::Android.Resource.Layout.SimpleListItem1, Sources);
myList.SetAdapter(adapter);
myList.ItemClick += ListClick; return view;
} void ListClick(object sender, AdapterView.ItemClickEventArgs e)
{
ListView list = sender as ListView;
string selectedFromList = list.GetItemAtPosition(e.Position).ToString();
}
}
}

使用ActionBar Tab的更多相关文章

  1. Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPager

    action来实现tab标签 并跟fragment结合 因为要写新闻客户端这个tab导航是必须的 这里我写几个小练习,希望大家融会贯通. 1actionbar设置tab +fragment 布局是个l ...

  2. ActionBar +Tab+ViewPager +Fragment 支持侧滑动完成办税工具的页面展示

    1:fragment_zhqrl.xml(征期日历) <?xml version="1.0" encoding="utf-8"?> <Line ...

  3. 15 ActionBar.Tab 以及保存fragment对象 代码案例

    API 21弃用 values 中 string文件源码: <?xml version="1.0" encoding="utf-8"?> <r ...

  4. Android 原生 Android ActionBar Tab (滑动)导航

    本文内容 环境 项目结构 演示一:ActionBar Tab 导航 演示二:ActionBar Tab 带滑动导航 本文演示 Tab 导航.第一个演示,是基本的 Tab 导航,第二个是带滑动的 Tab ...

  5. Android Actionbar Tab 导航模式

    Android Actionbar Tab 下图中,红色矩形圈起来的就是我们 ActionBar Tab,下面我们将一步一步的实现下图中的效果. 初次尝试 package com.example.it ...

  6. Android典型界面设计(6)——ActionBar Tab+ViewPager+Fagment实现滑动导航

    一.问题描述 在Android典型界面设计一文中,实现典型滑动导航界面,其实使用ActionBar 也可以轻松实现这一效果,甚至也可实现类似Android典型界面设计(3)的双导航效果.可见Actio ...

  7. actionbar tab 字体大小设置

    在styles.xml文件里加入以下的样式就可以 <!-- Application theme. -->     <style name="AppTheme" p ...

  8. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  9. Android ActionBar 关于tab的应用 以及 TabListener的方法详解

    actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...

随机推荐

  1. MVC5 + EF6 完整入门教程三:EF来了

    期待已久的EF终于来了 学完本篇文章,你将会掌握基于EF数据模型的完整开发流程. 本次将会完成EF数据模型的搭建和使用. 基于这个模型,将之前的示例添加数据库查询验证功能. 文章提纲 概述 & ...

  2. DOS常用命令总结

    cd 文件夹 如要进入D盘x文件夹,需要先d: 再Enter,然后cd x再Enter. echo \ & pause exit 换行等待按任意键退出,在此操作之前可以做发邮件等动作,bat调 ...

  3. php_html转译符号

    1.双引号 /" 或者 " 2.单引号 ' > 4. & &

  4. SQL指定字段指定顺序排序

    SELECT * FROM [ProjectInfo]ORDER BY (CASE DepartmentName WHEN 'AAA' THEN 1 WHEN 'BBB' THEN 2 WHEN 'C ...

  5. html、css基础注意点

    之前第一次接触html,一直使用table进行布局,十分麻烦还相当丑陋,造成当初并没有多大的兴趣,直到半年前开始接触到了使用div+css编写页面,才对它有了兴趣.作为一个菜鸟记录自己的点滴教训与收获 ...

  6. (原创)如何使用selenium 驱动chrome浏览器并且打开方式为手机模式-转载请注明出处

    随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要. 对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊. 使用chrome dr ...

  7. open nms安装教程

    而在正式的任务,我被要求在Windows平台上部署开源网络管理系统.虽然工作的任务,我得到了一些问题,对此我无法在网上寻找解决的问题,我用的命中和试验方法得到了解决.然后我想就这些问题及其解决办法写. ...

  8. ubuntu关于apache服务命令

    一.Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 start 二. ...

  9. 黑马程序员——C语言基础 指针

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)指针 首先指针是C语言中非常重要的数据类型,如果你说C语言中除了指针 ...

  10. AngularJs的UI组件ui-Bootstrap分享(十三)——Progressbar

    进度条控件有两种指令,第一种是uib-progressbar指令,表示单一颜色和进度的一个进度条.第二种是uib-bar和uib-progress指令,表示多种颜色和多个进度组合而成的一个进度条. 这 ...