[置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏
最近几天不忙,所以把项目中的顶部导航栏的实现归集一下。android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单。当然我这个导航栏是基于xamarin android的。废话不多说首先我们就来看看效果图
如果对于Fragment不太属性可以看看这篇文章 xamarin android Fragment实现底部导航栏。实现的主要步骤如下:
- Xamarin.Android TabLayout的简单使用
- Demo结构图
- 布局文件的介绍
- 主要Fragment类的介绍和FragmentPageAdapter的介绍
- MainActivity.cs 实现逻辑
1.xamarin android tabLayout的使用介绍
<LinearLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/color_primary">
<android.support.design.widget.TabLayout
android:id="@+id/tabMain"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabIndicatorColor="@color/color_white"
app:tabSelectedTextColor="@color/color_white"
app:tabIndicatorHeight="4dp"
app:tabTextColor="#dedede"
/>
</LinearLayout>
tablayout选中tabItem的标签样式的改变主要有这几个属性:
tabIndicator:选中标签提示条的颜色;tabSelectedTextColor:选中标签文本颜色;tabTextColor:默认文本颜色;tabIndicatorHeight:选中标签提示条的高度。不过有点尴尬的是不能直接设置选中标签文本大小。
TabLayout tab = FindViewById<TabLayout>(Resource.Id.tabMain);
tab.AddTab(tab.NewTab().SetText("热点"));
tab.AddTab(tab.NewTab().SetText("社会"));
tab.AddTab(tab.NewTab().SetText("体育"));
第一种方法在xml文件中写入TabItem。不过令人遗憾的是,按照java的写法居然报错。布局文件如下:
<android.support.design.widget.TabLayout
android:id="@+id/tabMain"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabIndicatorColor="@color/color_white"
app:tabSelectedTextColor="@color/color_white"
app:tabIndicatorHeight="4dp"
app:tabTextColor="#dedede">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabItem1"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabItem1"/>
</android.support.design.widget.TabLayout>
如果你觉得TabLayout自带的TabItem不符合你的审美,你也可以自定义一个:
2.项目结构图
1.布局文件的介绍
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="呵呵"
android:textColor="@color/color_primary"
android:textSize="20sp" />
</LinearLayout>
Main.axml代码如下,用的是相对定位,上方放的是tab,下方是ViewPager里填充Fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativelayout1"
android:background="@color/color_primary"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/color_primary"
android:layout_alignParentTop="true">
<android.support.design.widget.TabLayout
android:id="@+id/tabMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabIndicatorColor="@color/color_white"
app:tabSelectedTextColor="@color/color_white"
app:tabIndicatorHeight="4dp"
app:tabTextColor="#dedede" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dedede"
android:layout_below="@id/ly_top_bar">
<android.support.v4.view.ViewPager
android:id="@+id/ly_content"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
3.Fragment类的介绍和Viewpager数据适配器MyFragmentPageAdapter.cs的介绍
using Android.Views;
using Android.Widget;
using Fragment = Android.Support.V4.App.Fragment;
namespace FragmentDemo.Fragments
{
public class MyFragment2:Fragment
{
private string content { get; set; }
public MyFragment2(string content)
{
this.content = content;
}
public override View OnCreateView(LayoutInflater inflate,ViewGroup container,Bundle savedInstanceState)
{
View view = inflate.Inflate(Resource.Layout.fg_content_more,container,false);
TextView txt_content = view.FindViewById<TextView>(Resource.Id.txt_content);
txt_content.Text =content;
return view;
}
}
}
MyFragmentPagerAdapter.cs
using System;
using System.Collections.Generic;
using Android.Views;
using Android.Support.V4.App;
using Fragment = Android.Support.V4.App.Fragment;
namespace FragmentDemo.Fragments
{
public class MyFragmentPagerAdapter :FragmentPagerAdapter
{
private const int TabItemCount = 3;
private MyFragment myFragment = null;
private MyFragment2 myFragment2 = null;
private MyFragment3 myFragment3 = null;
public MainActivity MainActivity { get; set; }
private readonly List<Tuple<string, Type>> tabList = new List<Tuple<string,Type>>();
public MyFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm,int TabItemCount):base(fm)
{
AddTab<MyFragment>("热点");
AddTab<MyFragment2>("社会");
AddTab<MyFragment3>("体育");
}
public void AddTab<T>(string title)
{
tabList.Add(Tuple.Create(title,typeof(T)));
}
public override int Count
{
get
{
return TabItemCount;
}
}
public new string GetPageTitle(int position)
{
return tabList[position].Item1;
}
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
{
return base.InstantiateItem(container, position);
}
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object objectValue)
{
base.DestroyItem(container, position, objectValue);
}
public override Fragment GetItem(int position)
{
var type = tabList[position].Item2;
var retFragment = Activator.CreateInstance(type) as Android.Support.V4.App.Fragment;
return retFragment;
}
}
}
MyFragmentPagerAdapter.cs 我这里继承的是FragmentStatePagerAdapter,ViewPager要实现的就是将我们要显示的视图(Fragment)进行绑定,Viewpager有他自己的数据适配器PageAdapter.官方建议我们使用Fragment来填充ViewPager,给我们提供了两个适配器来管理生命周期,分别是FragmentPageAdapter和FragmentStatePagerAdapter
MainActivity.cs实现逻辑
MainActivity.cs 代码逻辑比较简单,就不多用文字描述
using Android.App;
using Android.Views;
using Android.OS;
using FragmentDemo.Fragments;
using Android.Support.V7.App;
using Android.Support.V4.View;
using Android.Support.Design.Widget;
namespace FragmentDemo
{
[Activity(Label = "FragmentDemo", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity
{
//UI objects
private ViewPager viewPager;
private MyFragmentPagerAdapter mAdapter;
//页面的常量
public const int PAGE_ONE = 0;
public const int PAGE_TWO = 1;
public const int PAGE_THREE = 2;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); TabLayout tab = FindViewById<TabLayout>(Resource.Id.tabMain);
tab.AddTab(tab.NewTab().SetText("热点"));
tab.AddTab(tab.NewTab().SetText("社会"));
tab.AddTab(tab.NewTab().SetText("体育")); tab.TabGravity = TabLayout.ModeScrollable;
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)
{
//透明状态栏
Window.AddFlags(WindowManagerFlags.TranslucentStatus);
//透明导航栏
Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}
viewPager = FindViewById<ViewPager>(Resource.Id.ly_content);
mAdapter = new MyFragmentPagerAdapter(SupportFragmentManager,tab.TabCount);
viewPager.Adapter = mAdapter;
viewPager.CurrentItem=0;
//txt_chat.Selected = true;
//Tab 选择事件
tab.TabSelected += (s, e) =>
{
viewPager.CurrentItem = e.Tab.Position;
};
viewPager.AddOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab));
}
}
}
作者:张林
标题:xamarin Tablayout+Viewpager+Fragment顶部导航栏 原文地址:http://blog.csdn.net/kebi007/article/details/70470754
转载随意注明出处
[置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏的更多相关文章
- [置顶]
bootstrap自定义样式-bootstrap侧边导航栏的实现
前言 bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的菜单,这就是我要实现的bootstrap自定义侧滑菜单 ...
- TabLayout+ViewPager制作简单导航栏
先看样例,有图有真相 绑定viewpager 此处主要说明tablayout的使用方法,viewpager绑定fragment的介绍在其他文章说明 mBinding.tabsLayout.setupW ...
- 使用PagerSlidingTabStrip实现顶部导航栏
使用PagerSlidingTabStrip配合ViewPager实现顶部导航栏. 效果图如下: PagerSlidingTabStrip是github上的一个开源项目,项目地址如下 ...
- Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- 使用TabLayout快速实现一个导航栏
在没有Material Design的年代,要实现一个类似微信主页面的效果,我们有以下几种解决方案: 1.Fragment + ViewPager + RadioGroup自定义固定导航条 2.F ...
- Android之仿今日头条顶部导航栏效果
随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候像今日头条新闻客户端要显示的内容太多,而且又想在主界面全部显示出来,所以 ...
- android开发(49) android 使用 CollapsingToolbarLayout ,可折叠的顶部导航栏
概述 在很app上都见过 可折叠的顶部导航栏效果.google support v7 提供了 CollapsingToolbarLayout 可以实现这个效果.效果图如下: 实现步骤 1. 写 ...
- 极致精简的fragment实现导航栏切换demo
一个小demo.用button+fragment实现导航栏切换界面,适合刚接触的新手看一下. 效果图 点击第二个后 源码: 主界面 <span style="font-size:18p ...
- 【React -- 9/100】 抽离顶部导航栏 - [组件复用]
今天写的页面中需要重复使用到顶部导航栏,所以把顶部导航栏抽离出来 考虑复用组件的健壮性,使用PropTypes校验,可以自定义一个click事件 JSX import React from " ...
随机推荐
- 机器学习算法--Perceptron(感知机)算法
感知机: 假设输入空间是\(\chi\subseteq R^n\),输出空间是\(\gamma =\left( +1,-1\right)\).输入\(\chi\in X\)表示实例的特征向量,对应于输 ...
- 安装apache
1.安装依赖软件 pcre gcc expat apr(Apache portable Run-time libraries,Apache可移植运行库) apr-util [root@localhos ...
- js 事件冒泡和事件捕获
事件流:指的是网页中元素接受事件的顺序,它是一个概念,而不是具体的实际的东西 事件冒泡:指的是内层元素的事件,会触发包含着此元素的外层元素的事件,触发的顺序是:由内而外的 例如: <!DOCTY ...
- Java(概略篇)
准备 下载并安装Java jdk 和 编辑器(eclipse或Netbeans) 配置环境变量 第一个程序 public class test{ public static void main(Str ...
- 找到链表的倒数第K位
#include<iostream> using namespace std; class node{ public: node():value(),next(NULL){} ~node( ...
- [行业关键词] review code review
意思是 代码评审 或是 代码回顾 代码评审是指在软件开发过程中,通过对源代码进行系统性检查的过程.通常的目的是查找系统缺陷,保证软件总体质量和提高开发者自身水平. Code Review是轻量级 ...
- node.js之express框架入门篇
一.express框架简介 express框架是后台的Node框架,在后台的受欢迎的程度,和jQuery一样 英语官网:http://expressjs.com/ 中文官网:http://www.ex ...
- TCP程序中发送和接收数据
这里我们来探讨一下在网络编程过程中,有关read/write 或者send/recv的使用细节.这里有关常用的阻塞/非阻塞的解释在网上有很多很好的例子,这里就不说了,还有errno ==EAGAIN ...
- linux crontab yum安装
crontab工具来做计划任务,定时任务,执行某个脚本等等 1.检查是否已安装crontab # crontab -bash: crontab: command not found 执行 cronta ...
- Ambari源代码分析之Resource.Type与ResourceProvider相应关系
前面提到.每一种Resource都相应一个ResourceProvider,以下给出其相应关系: Resource.Type ResourceProvider Workflow WorkflowRes ...