listview也許是用的很是頻繁的一個控件之一,下面我寫一個xamarin的listview栗子,你們嘗一嘗xamarin android開發的樂趣。原諒個人大小寫吧.javascript
listview綁定自定義的BaseAdapter
先來看一下最終實現的效果圖:php
News.cs 和NewAdapter.cscss
-
namespace
DrawerLayout.Adapter
-
{
-
public
class
News {
-
public
int Pv {
get;
set; }
-
public
string Title {
get;
set; }
-
public News(string title,int Pv)
-
{
-
this.Title = title;
-
this.Pv = Pv;
-
}
-
}
-
public
class
NewsAdapter :
BaseAdapter
-
{
-
private List<News> data;
-
private Context context;
-
public
override
int Count
-
{
-
get
-
{
-
return data.Count;
-
}
-
}
-
-
public NewsAdapter(List<News> data,Context context)
-
{
-
this.data = data;
-
this.context = context;
-
}
-
-
-
public
override Java.Lang.
Object GetItem(int position)
-
{
-
return
null;
-
}
-
-
public override long GetItemId(int position)
-
{
-
return position;
-
}
-
-
public override View GetView(int position, View convertView, ViewGroup parent)
-
{
-
convertView = LayoutInflater.From(context).Inflate(Resource.Layout.lv_test,parent,
false);
-
TextView title = convertView.FindViewById<TextView>(Resource.Id.tv_title);
-
TextView pv = convertView.FindViewById<TextView>(Resource.Id.tv_pv);
-
pv.Text = data[position].Pv.ToString();
-
title.Text = data[position].Title;
-
return convertView;
-
-
}
-
}
-
}
listview佈局lv_test.axmlhtml
-
<?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=
"#ffffff"
-
android:padding=
"10dp">
-
<LinearLayout
-
android:id=
"@+id/layout_content"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"60dp"
-
android:orientation=
"horizontal"
-
android:gravity=
"center_vertical">
-
<TextView
-
android:id=
"@+id/tv_title"
-
android:layout_height=
"50dp"
-
android:layout_width=
"0dp"
-
android:layout_weight=
"5"
-
android:textColor=
"#000000"
-
android:text=
"加內特的歷史地位比鄧肯差多少,差了一個艾佛森嗎?"
-
android:textSize=
"16dp" />
-
<TextView
-
android:id=
"@+id/tv_pv"
-
android:layout_height=
"40dp"
-
android:layout_width=
"0dp"
-
android:layout_weight=
"1"
-
android:textColor=
"#808080"
-
android:textSize=
"12dp"
-
android:text=
"19665"
-
-
android:gravity=
"right|center_vertical" />
-
</LinearLayout>
-
<View
-
android:layout_height=
"1dp"
-
android:layout_width=
"match_parent"
-
android:background=
"#dedede" />
-
</LinearLayout>
最後是MainActivity.csjava
-
namespace
DrawerLayout
-
{
-
[
Activity(Label = "ListViewDemo", MainLauncher = true, Icon = "@drawable/icon")]
-
public
class
MainActivity :
Activity
-
{
-
int count =
1;
-
private List<News> data;
-
private Context context;
-
private NewsAdapter adapter;
-
private ListView lv_test;
-
protected override void OnCreate(Bundle bundle)
-
{
-
base.OnCreate(bundle);
-
SetContentView(Resource.Layout.Main);
-
-
data =
new List<News>() {
-
new News (
"加內特的歷史地位能在NBA排第幾,超越德國戰車?",
1200),
-
new News (
"盤點新賽季最期待的十場比賽,無湖人比賽?",
560),
-
new News (
"庫裏新賽季鐵定無緣常規賽MVP",
158200),
-
new News (
"我服,庫裏,杜蘭特,湯普森誰纔是出手的最佳選擇",
900),
-
new News (
"易建聯的出場時間你能猜出來嗎,大概多少",
960),
-
new News (
"卡戴珊三姐妹睡多少男人",
960),
-
new News(
"科比退役後湖人到底失去多少中國的粉絲",
4986),
-
new News(
"科比退役湖人籤中國籃球當家背後隱藏了多少陰謀",
65987)
-
};
-
adapter =
new NewsAdapter(data,
this);
-
-
lv_test = FindViewById<ListView>(Resource.Id.lv_test);
-
//View lv_header = LayoutInflater.Inflate(Resource.Layout.lv_header, null);
-
//lv_test.AddHeaderView(lv_header);
-
lv_test.Adapter = adapter;
-
-
-
lv_test.ItemClick += (s, e) =>
-
{
-
OnClick(e.Position);
-
};
-
}
-
public void OnClick(int position)
-
{
-
position--;
-
Toast.MakeText(
this,
$"這條新聞有"+data[position].Pv+
"次瀏覽量",ToastLength.Short).Show();
-
}
-
}
-
}
一個最簡單的listview綁定數據就這麼簡單的。後面將會介紹幾個比較經常使用額屬性和方法python
2.xamarin android ListView表頭表尾分割線的設置:
listview能夠本身設置表頭表位,以及分割線,下面看一看具體的方法:mysql
- divider:設置分隔條,當設置爲@null時則沒有分隔條,設置的值能夠是顏色代碼,也能夠是drawable資源分割
- dividerHeight:設置分隔條的高度
- footDividersEnabled:是否在footerView 表尾前繪製一個分隔條,默認爲true
- headerDividerEnabled同上
這裏我參考的csdn-pig寫的android博客,設置listview 表頭表尾丙沒有這種屬性,只用如下幾個方法react
- AddHeaderView(View v):添加headerView 表頭,View v參數是一個佈局頁
- AddHeaderView(headerView,null,false):添加headerView 表頭,設置header是否能夠選中
- AddFooterView(View v):同上
- AddFooterView(headerView,null,false):同上
除了以上幾個屬性咱們還能夠設置listview的點擊效果等android
- stackFromBottom:設置列表放在最下面,默認爲的false
- cacheColorHint:若是你的Listview的Background是一張圖片的話,當你拖動和點擊ListView Item空白位置會發現item都變成黑色了,這個時候設置cacheColorHint設置爲透明#00000000 ,6個0的是黑色
- 隱藏滾動條 android:scrollbars="none
關於listview的簡單的用法就這麼了,固然這是最基礎的,還有不少問題須要去探索,listview 單擊項的效果,listview控件的優化。。。。。。web
listview demo 下載連接 : ListView例子源碼下載
有興趣的能夠關注一下個人微信公衆號,分享一些編程相關的經典文章