[置顶] 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 " ...
随机推荐
- laravel 表单验证 Exists 规则的基本使用方法
public function rules(){ return [ 'm_pushing_frequency_level_id' => 'integer|required|exists:m_pu ...
- web离线应用--dom storage
web离线应用--dom storage dom storage是html5添加的新功能,其实也不是什么新的应用,只不过是cookie的放大版本,由于cookie的大小只有4kb,而且在每次请求一个新 ...
- JavaScript的简单继承实现案例
<html><body><script> //实现JavaScript继承的步骤: //1:写父类 //2:写子类 //3:用Object.create()来实现继 ...
- 跨域请求cookie获取与设置问题
描述:最近做项目遇到了cookie的问题,项目为前后端分离项目,前台有分外网IP(A外)和内网IP(A内),后台服务只有一个内网IP(B内). 现象:当我前台异步请求发送给后台的时候,后台并不能获取到 ...
- 面向对象编程 —— java实现函数求导
文章目录 ★引子 ★求导 ★最初的想法 ★初步的想法 ★后来的想法 ★最后的想法 ★编程范式 ★结尾 首先声明一点,本文主要介绍的是面向对象(OO)的思想,顺便谈下函数式编程,而不是教你如何准确地.科 ...
- 常用IO按位操作
PORTD |= (1 << op) //位置高PORTD &= ~(1 << op) //位置低PORTD ^= (1 < ...
- html5本地存储数据,实现自动登录功能
背景: 在项目中遇到一个需求:用户登录完之后,网站自动保存用户id存储在本地,在下次打开网页时,通过id判断是否要进行登录操作. 技术方案: 在HTML5中,本地存储是一个window的属性,包括lo ...
- shell,bash,zsh,console,terminal到底是什么意思,它们之间又是什么关系?
原文链接 终端(terminal,或者叫物理终端):是一种设备,不是一个程序,一般说的就是能提供命令行用户界面的设备,典型的是屏幕和键盘,或其他的一些物理终端.虚拟终端:屏幕和键盘只是一个终端,可能不 ...
- Android APP 性能优化的一些思考
说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...
- windows配置git
每次要使用git指令的时候都要去打开git bash 操作,太麻烦,要想直接在dos窗口下使用git指令需要再进行如下环境变量配置. 1.系统环境变量path添加:D://programFiles/g ...