原创|使用教程|编辑:黄竹雯|2016-03-18 11:16:54.000|阅读 1207 次
概述:昨天我们已经一起学习了第1部分,这是探索Xamarin.Android的列表视图和适配器的的第2部分。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
昨天我们已经一起学习了第1部分,这是探索Xamarin.Android的列表视图和适配器的的第2部分。
在今天的文章中我们将探讨列表视图项排列使用BaseAdapter,还有自定义布局。
让我们深入到代码,看看ListView AXML和自定义项排列的AXML是什么样子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:tools="//schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:actionBarNavMode="tabs">
<ListView
android:id="@+id/moviesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:paddingLeft="5dp" />
<TextView
android:id="@+id/directedByTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00A14B"
android:paddingLeft="5dp" />
<TextView
android:id="@+id/releasedDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7F3F97"
android:paddingLeft="5dp" />
</LinearLayout>
让我们看看主要Activity是如何显示列表视图的
using Android.App;
using Android.OS;
using Android.Widget;
namespace AdapterDemo2
{
[Activity(Label = "AdapterDemo2",
MainLauncher = true,
Theme = "@android:style/Theme.Holo.Light",
Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var moviesListView = FindViewById<ListView>(Resource.Id.moviesListView);
moviesListView.ItemClick += moviesListView_ItemClick;
var moviesAdapter = new MovieAdapter(this, MoviesRepository.Movies);
moviesListView.Adapter = moviesAdapter;
}
void moviesListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
Toast.MakeText(this, MoviesRepository.Movies[e.Position].ToString(), ToastLength.Long).Show();
}
}
}
这个Activity与我们昨天讲到的主要区别是,我们不再使用ArrayAdapter,但我们现在有自己自定义的MovieAdapter类,并且列表视图的适配器设置为这个Adapter。
所以在MovieAdapter类中究竟有什么?让我们一起来探索:
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
namespace AdapterDemo2
{
public class MovieAdapter : BaseAdapter<Movie>
{
private readonly Activity context;
private readonly List<Movie> movies;
public MovieAdapter(Activity context, List<Movie> movies)
{
this.context = context;
this.movies = movies;
}
public override Movie this[int position]
{
get
{
return movies[position];
}
}
public override int Count
{
get
{
return movies.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate(Resource.Layout.MovieRow, parent, false);
}
var titleTextView = view.FindViewById<TextView>(Resource.Id.titleTextView);
var directedByTextView = view.FindViewById<TextView>(Resource.Id.directedByTextView);
var releasedDateTextView = view.FindViewById<TextView>(Resource.Id.releasedDateTextView);
titleTextView.Text = movies[position].Title;
directedByTextView.Text = "Directed by: " + movies[position].Director;
releasedDateTextView.Text = "Released on: " + movies[position].ReleaseDate.ToShortDateString();
return view;
}
}
}
继承自BaseAdapter的MovieAdapter类主要定义了4种方法,都是BaseAdapter抽象类所要求的。他们是:
GetItemId给你一个选项让列表视图知道position在查找哪个当前项的id。
Count属性非常直接,它告诉我们列表视图目前显示了多少项。
这是.NET的数组索引器过载的方法,使对象在一个给定的position。
GetView方法是在一个适配器中使用的最重要的方法。GetView的实现始于获取它将要处理的视图。Android中的所有视图过多地使用LayoutInflater和GetView方法也不例外。使用LayoutInflater时,我们将会在我们的环境中定义Layout、MovieRow。
一旦我们认为我们已经熟悉和设置文本以及视图中可能的其他属性,我们就可以使用常规的FindViewById方法。
现在让我们运行应用程序,看看是什么样子:

哦,漂亮!这仅仅是一个定制的列表视图和适配器能做到的开始,而你能做的机会却是无限的。
如果有很多显示的数据,那么列表视图的快速滚动是非常有用的。通过快速滚动,你可以拖动滚动条来更快速完成大量的数据。

启用快速滚动。你只需:
moviesListView.FastScrollEnabled = true;
现在有快速滚动已经很不错了,但会使其容易地滚动很多行数据的被称为部分索引。启用部分索引,你将会从“ISectionIndexer”继承你的Activity 。
让我们看看ISectionIndexer授权的方法实现后适配器看起来是什么样子:
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
namespace AdapterDemo2
{
public class MovieAdapter : BaseAdapter<Movie>, ISectionIndexer
{
private readonly Activity context;
private readonly List<Movie> movies;
public MovieAdapter(Activity context, List<Movie> movies)
{
this.context = context;
this.movies = movies;
}
public override Movie this[int position]
{
get
{
return movies[position];
}
}
public override int Count
{
get
{
return movies.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate(Resource.Layout.MovieRow, parent, false);
}
var titleTextView = view.FindViewById<TextView>(Resource.Id.titleTextView);
var directedByTextView = view.FindViewById<TextView>(Resource.Id.directedByTextView);
var releasedDateTextView = view.FindViewById<TextView>(Resource.Id.releasedDateTextView);
titleTextView.Text = movies[position].Title;
directedByTextView.Text = "Directed by: " + movies[position].Director;
releasedDateTextView.Text = "Released on: " + movies[position].ReleaseDate.ToShortDateString();
return view;
}
Java.Lang.Object[] sectionHeaders = SectionIndexerBuilder.BuildSectionHeaders(MoviesRepository.Movies);
Dictionary<int, int> positionForSectionMap = SectionIndexerBuilder.BuildPositionForSectionMap(MoviesRepository.Movies);
Dictionary<int, int> sectionForPositionMap = SectionIndexerBuilder.BuildSectionForPositionMap(MoviesRepository.Movies);
public Java.Lang.Object[] GetSections()
{
return sectionHeaders;
}
public int GetPositionForSection(int section)
{
return positionForSectionMap[section];
}
public int GetSectionForPosition(int position)
{
return sectionForPositionMap[position];
}
}
}
activity应该实现的部分索引器方法:
让Android知道列表视图应该显示的所有部分。
取得一个给定部分的整数位置。
取得一个给定位置的部分。
现在所有这三种方法都利用SectionIndexBuilder——我借用的Xamarin教程的类。让我们看看SectionIndexBuilder是什么
using System.Collections.Generic;
namespace AdapterDemo2
{
public static class SectionIndexerBuilder
{
// builds an array of unique section headers, data must be sorted by name
public static Java.Lang.Object[] BuildSectionHeaders(List<Movie> data)
{
var results = new List<string>();
var used = new SortedSet<string>();
foreach (var item in data)
{
var letter = item.Title[0].ToString();
if (!used.Contains(letter))
results.Add(letter);
used.Add(letter);
}
var jobjects = new Java.Lang.Object[results.Count];
for (int i = 0; i < results.Count; i++)
{
jobjects[i] = results[i];
}
return jobjects;
}
// builds a map to answer: position --> section, data must be sorted by name
public static Dictionary<int, int> BuildSectionForPositionMap(List<Movie> movies)
{
var results = new Dictionary<int, int>();
var used = new SortedSet<string>();
int section = -1;
for (int i = 0; i < movies.Count; i++)
{
var letter = movies[i].Title[0].ToString();
if (!used.Contains(letter))
{
section++;
used.Add(letter);
}
results.Add(i, section);
}
return results;
}
// builds a map to answer: section --> position, data must be sorted by name
public static Dictionary<int, int> BuildPositionForSectionMap(List<Movie> movies)
{
var results = new Dictionary<int, int>();
var used = new SortedSet<string>();
int section = -1;
for (int i = 0; i < movies.Count; i++)
{
var letter = movies[i].Title[0].ToString();
if (!used.Contains(letter))
{
section++;
used.Add(letter);
results.Add(section, i);
}
}
return results;
}
}
}
SectionIndexBuilder确实确实做了一些聪明的事,通过列表数据找出一个给定的数据列表的部分和位置。
如果你现在运行应用程序,你应该可以开始看到“部分”,这使它更容易为你的用户滚动大量列表数据。

这就是Xamarin.Android的列表视图和适配器。下次我会继续和大家探索新的教程,敬请期待!
Xamarin正式被Microsoft收购,慧都将为您提供更好的解决方案和服务!详情请<>
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都控件网