xamarin.Android 实现横向滚动导航
经过一段时间的练习,自己也做了不少的demo和一些小项目,今天就把这些demo分享给大家,也当做笔记记录到自己的博客中。
这次给大家带来的是HorizontalScrollView实现横向滑动,参考博客http://fangyong2006.iteye.com/blog/2035093?utm_source=tuicool。
先让大家简单的了解一下什么是HorizontalScrollView。
HorizontalScrollView 是一种 框架布局。HorizontalScrollView是一个 FrameLayout , 这意味着你只能在它下面放置一个子控件 ,这个子控件可以包含很多数据内容。允许视图结构比手机的屏幕大。 这个布局控件一般使用的是一个水平布局的 LinearLayout 。 文本视图 类也有其自身的滚动处理,不需要嵌入滚动视图; 但二者可以组合使用,其效果与将文本视图放入很大容器中一样. HorizontalScrollView 只支持水平方向的滚动。 HorizontalScrollView不可以和ListView同时用。
下面我分三个步骤完成个demo,视图,适配器,主体代码。
视图Main.axml
<?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">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<HorizontalScrollView
android:id="@+id/category_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dip"
android:scrollbars="none">
<LinearLayout
android:id="@+id/category_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" />
</HorizontalScrollView>
</RelativeLayout>
</LinearLayout>
Text.axml
<?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">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_gravity="center" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvbutn"
android:layout_gravity="center" />
</LinearLayout>
适配器TitleAdapter
class TitleAdapter : BaseAdapter
{
private Context context;
private List<String> titles;
public TitleAdapter(Context context, List<String> strings)
{
this.context = context;
titles = strings;
} public override int Count
{
get { return titles.Count; }
} public override Java.Lang.Object GetItem(int position)
{
return null;
} public override long GetItemId(int position)
{
return position;
} public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
convertView = LayoutInflater.From(context).Inflate(Resource.Layout.Text, null);
ImageView img = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
TextView title = convertView.FindViewById<TextView>(Resource.Id.tvbutn);
title.Text = titles[position].ToString();
if (title.Text == titles[].ToString())
{
title.SetTextColor(Color.Red);
}
return convertView;
}
}
最后的MainActivity
public class Activity1 : Activity, AdapterView.IOnItemClickListener, View.IOnClickListener
{
/* 导航菜单集合 */
private List<String> array;
/* 导航菜单适配器 */
private TitleAdapter titleAdapter;
/* 列宽 */
private int COLUMNWIDTH = ;
/* 导航菜单布局 */
private GridView category;
/* 导航菜单容器,存放导航菜单布局 */
private LinearLayout categoryLayout;
private HorizontalScrollView horizontalScrollView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
FindViews();
setListeners();
initViews();
} private void FindViews()
{
horizontalScrollView = FindViewById<HorizontalScrollView>(Resource.Id.category_scrollview);
categoryLayout = FindViewById<LinearLayout>(Resource.Id.category_layout);
// 新建一个GridView
category = new GridView(ApplicationContext);
}
private void setListeners()
{
// 设置GridView的点击事件
category.OnItemClickListener = this;
}
private void initViews()
{
array = new List<string>();
array.Add("一号标题");
array.Add("二号标题");
array.Add("三号标题");
array.Add("四号标题");
// 最简单的一个适配器,里面就一个TextView
titleAdapter = new TitleAdapter(ApplicationContext, array);
// 设置内部子栏目的宽度
category.SetColumnWidth(COLUMNWIDTH);
// 设置内部子栏目个数为自动适应
category.SetNumColumns(array.Count);
// 设置Gravity为Center
category.SetGravity(GravityFlags.Center);
// 设置Selector为透明
category.Selector = new ColorDrawable(Color.Transparent);
int width = COLUMNWIDTH * array.Count;
Android.Widget.LinearLayout.LayoutParams layoutParams = new Android.Widget.LinearLayout.LayoutParams(width, Android.Widget.LinearLayout.LayoutParams.WrapContent);
// 设置GridView的LayoutParams为子栏目的宽度乘以栏目个数
category.LayoutParameters = layoutParams;
// 设置适配器
category.Adapter = titleAdapter;
// 将新建的GridView添加到布局中
categoryLayout.AddView(category);
}
public void OnClick(View v)
{
horizontalScrollView.Fling();
} public void OnItemClick(AdapterView parent, View view, int position, long id)
{
TextView categoryTitle;
// 每次都循环的将子栏目的颜色和背景还原
for (int i = ; i < parent.Count; i++)
{
categoryTitle = (TextView)parent.GetChildAt(i);
categoryTitle.SetTextColor(Color.White);
categoryTitle.SetBackgroundDrawable(null);
} categoryTitle = (TextView)view;
categoryTitle.SetTextColor(Color.Red);
//Tle.Text = categoryTitle.Text;
}
}
代码大致都差不多,分享过来给大家参考,最后的效果也就和上述博客差不多。
xamarin.Android 实现横向滚动导航的更多相关文章
- [置顶]
xamarin android Fragment实现底部导航栏
前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...
- Android TextView 横向滚动(跑马灯效果)
Android TextView 中当文字比較多时希望它横向滚动显示,以下是一种亲測可行的方法. 效果图: 1.自己定义TextView,重写isFocused()方法返回true,让自己定义Text ...
- Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
本博主在一次个人移动端项目中,遇到这么一个需求:希望自己的项目中,头部导航条的效果可以像今日头条那样,横向滚动! 对于这样的效果,在各大移动端项目中几乎是随处可见,为什么呢? 我们都知道,对于移动端也 ...
- 仿今日头条横向滚动导航栏--原生js
咳咳!先打一波小广告,在上一篇里忘记了,那啥……我的那个个人博客做好了-->(我的博客)<--.嘿嘿 好嘞,言归正传,说说我们的效果. 其实就是实现横向滑动,进行选择. 原理: 鼠标按下, ...
- [置顶]
xamarin Tablayout+Viewpager+Fragment顶部导航栏
最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- 【吉光片羽】js横向滚动与浮动导航
1.横向滚动,这个方法是见过最简洁的了. #demo { background: #FFF; overflow: hidden; border: 1px dashed #CCC; width: 117 ...
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...
- Android 横向列表GridView 实现横向滚动
Android 横向列表实现,可左右滑动,如下图 1.主界面布局代码:activity_main.xml a.包裹HorizontalScrollView控件是GirdView横向滚动的基本条件b.G ...
随机推荐
- ASP.NET(五):ASP.net实现真分页显示数据
导读:在上篇文章中,介绍了用假分页实现数据的分页显示 ,而避免了去拖动滚动条.但,假分页在分页的同时,其实是拖垮了查询效率的.每一次分页都得重新查询一遍数据,那么有没有方法可以同时兼顾效率和分页呢,那 ...
- nginx中access_log和nginx.conf中的log_format用法
nginx服务器日志相关指令主要有两条: 一条是log_format,用来设置日志格式; 另外一条是access_log,用来指定日志文件的存放路径.格式和缓存大小 可以参加ngx_http_log_ ...
- UITableView加载几种不同的cell
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- BZOJ 3238 [Ahoi2013]差异 ——后缀自动机
后缀自动机的parent树就是反串的后缀树. 所以只需要反向构建出后缀树,就可以乱搞了. #include <cstdio> #include <cstring> #inclu ...
- [USACO08DEC]Trick or Treat on the Farm (拓扑排序,DP)
题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...
- Linux(9):期中架构(1)--- 集群构架 & 备份服务
01. 了解集群架构服务器组成 基本架构组成:(用于让用户进行访问) # 前端服务部分: 1)顾客-用户 是一个访问者,请求访问网站页面 2)保安-防火墙设备 对访问架构用户进行策略控制,正常访问网站 ...
- [C++] 频谱图中 FFT快速傅里叶变换C++实现
在项目中,需要画波形频谱图,因此进行查找,不是很懂相关知识,下列代码主要是针对这篇文章. http://blog.csdn.net/xcgspring/article/details/4749075 ...
- Yii 之Session使用
public function actionIndex(){ $session = \YII::$app->session; //判断session是否开启 if(!$session->i ...
- 代码布局relativeLayout
后台布局 在ANDROID 开发中有时候我们需要在后台添加布局文件这里我们来说一下后台添加RelativeLayout文件的方式: RelativeLayout,顾名思义,就是以“相对”位置/对齐 ...
- 使用nginx时,让web取得原始请求地址
问题描述 当使用nginx配置proxy_pass参数以后,后端web取得的Request.Uri是proxy_pass中配置的地址,而不是client访问的原始地址 举例说明: 假设nginx配置文 ...