[置顶] 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 " ...
随机推荐
- 日志的艺术(The art of logging)
程序员学习每一门语言都是从打印“hello world”开始的,日志也是新手程序员学习.调试程序的一大利器.当项目上线之后,也会有各种各样的日志,比如记录用户的行为.服务器的状态.异常情况等等.打印日 ...
- canvas入门之时钟的实现
canvas 入门之作: 三步实现一个时钟: 直接上效果: step 1 : 背景制作首先制作从1-12的数字: var canvas = document.getElementById('ca ...
- CCF-201312-3-最大的矩形
问题描述 试题编号: 201312-3 试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ ...
- CCF-201503-2-数字排序
问题描述 试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序 ...
- 十一招让Ubuntu 16.04用起来更得心应手(转)
ubuntu 16.04是一种长期支持版本(LTS),是Canonical承诺发布五年的更新版.也就是说,你可以让这个版本在电脑上运行五年! 这样一来,一开始就设置好显得特别重要.你应该确保你的软件是 ...
- Atcoder R84 D Small Multiple
题意:给定一个正整数K,求K的倍数中,各位上的数字之和最小是多少? 思路非常巧妙,对于一个数,我们有定义两种改变方式: 1.加1,则数字之和+1(9的情况另行考虑) 2.乘10,数字之和不变 对于末位 ...
- NGUI_Button
十.按钮,Button 1.按钮的核心作用: 按钮能够接收单击并触发响应事件 按钮单击时能同时触发多个响应事件 按钮可以有普通.悬停.单击.禁用等多个状态的不同表现 广泛的说,按钮的核心在于接收事件 ...
- Java SE 8 流库(二)
1.3. filter,map,flatMAP方法 流的转换会产生一个新流,它的元素派生出自另一个流中的元素: Stream<T> filter(Predicate<? super ...
- ML—高斯判别分析
华电北风吹 天津大学认知计算与应用重点实验室 日期:2015/12/11 高斯判别分析属于生成模型,模型终于学习一个特征-类别的联合概率. 0 多维正态分布 确定一个多维正态分布仅仅须要知道分布的均值 ...
- 【并查集】HDU 1325 Is It A Tree?
推断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no ...