经过一段时间的练习,自己也做了不少的demo和一些小项目,今天就把这些demo分享给大家,也当做笔记记录到自己的博客中。

这次给大家带来的是HorizontalScrollView实现横向滑动,参考博客http://fangyong2006.iteye.com/blog/2035093?utm_source=tuicool。

先让大家简单的了解一下什么是HorizontalScrollView。

HorizontalScrollView 是一种 框架布局。HorizontalScrollView是一个 FrameLayout , 这意味着你只能在它下面放置一个子控件 ,这个子控件可以包含很多数据内容。允许视图结构比手机的屏幕大。 这个布局控件一般使用的是一个水平布局的 LinearLayout 。 文本视图 类也有其自身的滚动处理,不需要嵌入滚动视图; 但二者可以组合使用,其效果与将文本视图放入很大容器中一样. HorizontalScrollView 只支持水平方向的滚动。 HorizontalScrollView不可以和ListView同时用。

下面我分三个步骤完成个demo,视图,适配器,主体代码。

视图Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:layout_marginTop="10dip">
  10. <HorizontalScrollView
  11. android:id="@+id/category_scrollview"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_centerVertical="true"
  15. android:layout_marginLeft="6dip"
  16. android:scrollbars="none">
  17. <LinearLayout
  18. android:id="@+id/category_layout"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:gravity="center_vertical"
  22. android:orientation="horizontal" />
  23. </HorizontalScrollView>
  24. </RelativeLayout>
  25. </LinearLayout>

Text.axml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <ImageView
  9. android:src="@android:drawable/ic_menu_gallery"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/imageView1"
  13. android:layout_gravity="center" />
  14. <TextView
  15. android:text="Text"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:id="@+id/tvbutn"
  19. android:layout_gravity="center" />
  20. </LinearLayout>

适配器TitleAdapter

  1. class TitleAdapter : BaseAdapter
  2. {
  3. private Context context;
  4. private List<String> titles;
  5. public TitleAdapter(Context context, List<String> strings)
  6. {
  7. this.context = context;
  8. titles = strings;
  9. }
  10.  
  11. public override int Count
  12. {
  13. get { return titles.Count; }
  14. }
  15.  
  16. public override Java.Lang.Object GetItem(int position)
  17. {
  18. return null;
  19. }
  20.  
  21. public override long GetItemId(int position)
  22. {
  23. return position;
  24. }
  25.  
  26. public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
  27. {
  28. convertView = LayoutInflater.From(context).Inflate(Resource.Layout.Text, null);
  29. ImageView img = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
  30. TextView title = convertView.FindViewById<TextView>(Resource.Id.tvbutn);
  31. title.Text = titles[position].ToString();
  32. if (title.Text == titles[].ToString())
  33. {
  34. title.SetTextColor(Color.Red);
  35. }
  36. return convertView;
  37. }
  38. }

最后的MainActivity

  1. public class Activity1 : Activity, AdapterView.IOnItemClickListener, View.IOnClickListener
  2. {
  3. /* 导航菜单集合 */
  4. private List<String> array;
  5. /* 导航菜单适配器 */
  6. private TitleAdapter titleAdapter;
  7. /* 列宽 */
  8. private int COLUMNWIDTH = ;
  9. /* 导航菜单布局 */
  10. private GridView category;
  11. /* 导航菜单容器,存放导航菜单布局 */
  12. private LinearLayout categoryLayout;
  13. private HorizontalScrollView horizontalScrollView;
  14. protected override void OnCreate(Bundle bundle)
  15. {
  16. base.OnCreate(bundle);
  17. SetContentView(Resource.Layout.Main);
  18. FindViews();
  19. setListeners();
  20. initViews();
  21. }
  22.  
  23. private void FindViews()
  24. {
  25. horizontalScrollView = FindViewById<HorizontalScrollView>(Resource.Id.category_scrollview);
  26. categoryLayout = FindViewById<LinearLayout>(Resource.Id.category_layout);
  27. // 新建一个GridView
  28. category = new GridView(ApplicationContext);
  29. }
  30. private void setListeners()
  31. {
  32. // 设置GridView的点击事件
  33. category.OnItemClickListener = this;
  34. }
  35. private void initViews()
  36. {
  37. array = new List<string>();
  38. array.Add("一号标题");
  39. array.Add("二号标题");
  40. array.Add("三号标题");
  41. array.Add("四号标题");
  42. // 最简单的一个适配器,里面就一个TextView
  43. titleAdapter = new TitleAdapter(ApplicationContext, array);
  44. // 设置内部子栏目的宽度
  45. category.SetColumnWidth(COLUMNWIDTH);
  46. // 设置内部子栏目个数为自动适应
  47. category.SetNumColumns(array.Count);
  48. // 设置Gravity为Center
  49. category.SetGravity(GravityFlags.Center);
  50. // 设置Selector为透明
  51. category.Selector = new ColorDrawable(Color.Transparent);
  52. int width = COLUMNWIDTH * array.Count;
  53. Android.Widget.LinearLayout.LayoutParams layoutParams = new Android.Widget.LinearLayout.LayoutParams(width, Android.Widget.LinearLayout.LayoutParams.WrapContent);
  54. // 设置GridView的LayoutParams为子栏目的宽度乘以栏目个数
  55. category.LayoutParameters = layoutParams;
  56. // 设置适配器
  57. category.Adapter = titleAdapter;
  58. // 将新建的GridView添加到布局中
  59. categoryLayout.AddView(category);
  60. }
  61. public void OnClick(View v)
  62. {
  63. horizontalScrollView.Fling();
  64. }
  65.  
  66. public void OnItemClick(AdapterView parent, View view, int position, long id)
  67. {
  68. TextView categoryTitle;
  69. // 每次都循环的将子栏目的颜色和背景还原
  70. for (int i = ; i < parent.Count; i++)
  71. {
  72. categoryTitle = (TextView)parent.GetChildAt(i);
  73. categoryTitle.SetTextColor(Color.White);
  74. categoryTitle.SetBackgroundDrawable(null);
  75. }
  76.  
  77. categoryTitle = (TextView)view;
  78. categoryTitle.SetTextColor(Color.Red);
  79. //Tle.Text = categoryTitle.Text;
  80. }
  81. }

代码大致都差不多,分享过来给大家参考,最后的效果也就和上述博客差不多。

xamarin.Android 实现横向滚动导航的更多相关文章

  1. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  2. Android TextView 横向滚动(跑马灯效果)

    Android TextView 中当文字比較多时希望它横向滚动显示,以下是一种亲測可行的方法. 效果图: 1.自己定义TextView,重写isFocused()方法返回true,让自己定义Text ...

  3. Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条

    本博主在一次个人移动端项目中,遇到这么一个需求:希望自己的项目中,头部导航条的效果可以像今日头条那样,横向滚动! 对于这样的效果,在各大移动端项目中几乎是随处可见,为什么呢? 我们都知道,对于移动端也 ...

  4. 仿今日头条横向滚动导航栏--原生js

    咳咳!先打一波小广告,在上一篇里忘记了,那啥……我的那个个人博客做好了-->(我的博客)<--.嘿嘿 好嘞,言归正传,说说我们的效果. 其实就是实现横向滑动,进行选择. 原理: 鼠标按下, ...

  5. [置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏

    最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...

  6. Xamarin Android Fragment的两种加载方式

    android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...

  7. 【吉光片羽】js横向滚动与浮动导航

    1.横向滚动,这个方法是见过最简洁的了. #demo { background: #FFF; overflow: hidden; border: 1px dashed #CCC; width: 117 ...

  8. Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》

    Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...

  9. Android 横向列表GridView 实现横向滚动

    Android 横向列表实现,可左右滑动,如下图 1.主界面布局代码:activity_main.xml a.包裹HorizontalScrollView控件是GirdView横向滚动的基本条件b.G ...

随机推荐

  1. BZOJ 1260: [CQOI2007]涂色paint【区间DP】

    Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...

  2. jenkins异常解答

    1.安装插件时offline 需要更换插件管理中的升级URL   http://mirror.xmission.com/jenkins/updates/current/update-center.js ...

  3. Centos 安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  4. 货车运输(codevs 3287)

    题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过 ...

  5. Requests模拟登陆

    requests模拟登陆知乎网站 实例 # -*- coding: utf-8 -*- __author__ = 'CQ' import requests try: import cookielib ...

  6. golang导出Excel表格

    设置样式: package main import ( "github.com/tealeg/xlsx" "fmt" ) func main() { var f ...

  7. 如何选择 IT 技术书籍

    ★第1招:看网上评论 首先,上一些权威的图书网站,看看大伙儿的评价如何(要相信群众的眼睛是雪亮的).对于英文书籍,我一般上亚马逊网站去看看:中文书籍则上豆瓣网.这两个网站都提供星级评分,一般 > ...

  8. 非旋转Treap:用运行时间换调试时间的有效手段

    非旋转Treap:用运行时间换调试时间的有效手段   Hello大家好,我们今天来聊一聊非旋转Treap. 相信各位或多或少都做过些序列上的问题.如果水题我们考虑暴力:不强制在线我们可能用过莫队和待修 ...

  9. codevs——2370 小机房的树

    2370 小机房的树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 小机房有棵焕狗种的树,树上有N个 ...

  10. codevs——1958 刺激

    1958 刺激  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺 ...