Xamarin. Android for horizontal scrolling navigation
Last Update:2015-06-19
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">After a period of practice, I have also done a lot of demo and some small projects, today to share these demos to everyone, but also as notes recorded in their own blog.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">This brings us horizontalscrollview to achieve horizontal sliding, refer to the blog Http://fangyong2006.iteye.com/blog/2035093?utm_source=tuicool.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">Let's start with a simple look at what is HORIZONTALSCROLLVIEW.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">Horizontalscrollview is a framework Layout. Horizontalscrollview is a framelayout, which means that you can only place a child control underneath it, which can contain a lot of data content. Allows the view structure to be larger than the Phone's Screen. This layout control typically uses a horizontal layout of the Linearlayout. The text view class also has its own scrolling, which does not need to be embedded in a scrolling view, but can be used in combination with the same effect as putting a text view into a large container. The Horizontalscrollview only supports horizontal scrolling. Horizontalscrollview can not be used in conjunction with the LISTVIEW.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">Below I have three steps to complete a demo, view, adapter, body Code.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 13px;">View Main.axml</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "fill_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "fill_parent"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Relativelayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Xmlns:tools</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/tools"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_margintop</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "10dip"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Horizontalscrollview</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/category_scrollview"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:layout_centervertical</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "true"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_marginleft</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "6dip"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:scrollbars</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "none"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/category_layout"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:gravity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "center_vertical"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "horizontal"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Horizontalscrollview</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Relativelayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Text.axml</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "fill_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "fill_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:minwidth</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "25px"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:minheight</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "25px"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">ImageView</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:src</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@android:d rawable/ic_menu_gallery"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/imageview1"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:layout_gravity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "center"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TextView</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "Text"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/tvbutn"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:layout_gravity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "center"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Adapter Titleadapter</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Titleadapter:baseadapter {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Context context; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>List<string><span style="color: #000000;"><span style="color: #000000;">titles; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Titleadapter (context context, list<string><span style="color: #000000;"><span style="color: #000000;">Strings) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Context =<span style="color: #000000;"><span style="color: #000000;">context; Titles</span></span>=<span style="color: #000000;"><span style="color: #000000;">strings; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Count {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Get</span></span>{<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">Titles. Count; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Java.Lang.Object GetItem (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Position) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span>Getitemid (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Position) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">position; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Android.Views.View GetView (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">position, Android.Views.View convertview, Android.Views.ViewGroup Parent) {convertview</span></span>= Layoutinflater.from (context). Inflate (Resource.Layout.Text,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); ImageView img</span></span>= convertview.findviewbyid<imageview><span style="color: #000000;"><span style="color: #000000;">(Resource.Id.imageView1); TextView title</span></span>= convertview.findviewbyid<textview><span style="color: #000000;"><span style="color: #000000;">(Resource.Id.tvbutn); Title. Text</span></span>=<span style="color: #000000;"><span style="color: #000000;">titles[position]. ToString (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(title. Text = = titles[<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]. ToString ()) {title. SetTextColor (color.red); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">convertview; } }</span></span></pre><p><p>The last Mainactivity</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">activity1:activity, adapterview.ionitemclicklistener, View.ionclicklistener {</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">navigation Menu Collection</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>List<string><span style="color: #000000;"><span style="color: #000000;">array; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">navigation Menu Adapter</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Titleadapter titleadapter; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">Column Width</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>COLUMNWIDTH =<span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">navigation Menu Layout</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">GridView category; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">navigation menu container, storing navigation menu layout</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">LinearLayout categorylayout; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Horizontalscrollview horizontalscrollview; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnCreate (bundle Bundle) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Base</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (bundle); Setcontentview (Resource.Layout.Main); Findviews (); Setlisteners (); Initviews (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">findviews () {horizontalscrollview</span></span>= findviewbyid