xamarin android listview的用法(轉載 https://blog.csdn.net/kebi007/article/details/52650729)

xamarin android listview的用法

版權聲明:本文爲博主原創文章,未經博主容許轉載隨意。 https://blog.csdn.net/kebi007/article/details/52650729

listview也許是用的很是頻繁的一個控件之一,下面我寫一個xamarin的listview栗子,你們嘗一嘗xamarin android開發的樂趣。原諒個人大小寫吧.javascript

listview綁定自定義的BaseAdapter

先來看一下最終實現的效果圖:php

News.cs 和NewAdapter.cscss

      
      
      
      
      
  1. namespace DrawerLayout.Adapter
  2. {
  3. public class News {
  4. public int Pv { get; set; }
  5. public string Title { get; set; }
  6. public News(string title,int Pv)
  7. {
  8. this.Title = title;
  9. this.Pv = Pv;
  10. }
  11. }
  12. public class NewsAdapter : BaseAdapter
  13. {
  14. private List<News> data;
  15. private Context context;
  16. public override int Count
  17. {
  18. get
  19. {
  20. return data.Count;
  21. }
  22. }
  23. public NewsAdapter(List<News> data,Context context)
  24. {
  25. this.data = data;
  26. this.context = context;
  27. }
  28. public override Java.Lang. Object GetItem(int position)
  29. {
  30. return null;
  31. }
  32. public override long GetItemId(int position)
  33. {
  34. return position;
  35. }
  36. public override View GetView(int position, View convertView, ViewGroup parent)
  37. {
  38. convertView = LayoutInflater.From(context).Inflate(Resource.Layout.lv_test,parent, false);
  39. TextView title = convertView.FindViewById<TextView>(Resource.Id.tv_title);
  40. TextView pv = convertView.FindViewById<TextView>(Resource.Id.tv_pv);
  41. pv.Text = data[position].Pv.ToString();
  42. title.Text = data[position].Title;
  43. return convertView;
  44. }
  45. }
  46. }

listview佈局lv_test.axmlhtml

 

 

      
      
      
      
      
  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= "match_parent"
  5. android:layout_height= "match_parent"
  6. android:background= "#ffffff"
  7. android:padding= "10dp">
  8. <LinearLayout
  9. android:id= "@+id/layout_content"
  10. android:layout_width= "match_parent"
  11. android:layout_height= "60dp"
  12. android:orientation= "horizontal"
  13. android:gravity= "center_vertical">
  14. <TextView
  15. android:id= "@+id/tv_title"
  16. android:layout_height= "50dp"
  17. android:layout_width= "0dp"
  18. android:layout_weight= "5"
  19. android:textColor= "#000000"
  20. android:text= "加內特的歷史地位比鄧肯差多少,差了一個艾佛森嗎?"
  21. android:textSize= "16dp" />
  22. <TextView
  23. android:id= "@+id/tv_pv"
  24. android:layout_height= "40dp"
  25. android:layout_width= "0dp"
  26. android:layout_weight= "1"
  27. android:textColor= "#808080"
  28. android:textSize= "12dp"
  29. android:text= "19665"
  30. android:gravity= "right|center_vertical" />
  31. </LinearLayout>
  32. <View
  33. android:layout_height= "1dp"
  34. android:layout_width= "match_parent"
  35. android:background= "#dedede" />
  36. </LinearLayout>

最後是MainActivity.csjava

      
      
      
      
      
  1. namespace DrawerLayout
  2. {
  3. [ Activity(Label = "ListViewDemo", MainLauncher = true, Icon = "@drawable/icon")]
  4. public class MainActivity : Activity
  5. {
  6. int count = 1;
  7. private List<News> data;
  8. private Context context;
  9. private NewsAdapter adapter;
  10. private ListView lv_test;
  11. protected override void OnCreate(Bundle bundle)
  12. {
  13. base.OnCreate(bundle);
  14. SetContentView(Resource.Layout.Main);
  15. data = new List<News>() {
  16. new News ( "加內特的歷史地位能在NBA排第幾,超越德國戰車?", 1200),
  17. new News ( "盤點新賽季最期待的十場比賽,無湖人比賽?", 560),
  18. new News ( "庫裏新賽季鐵定無緣常規賽MVP", 158200),
  19. new News ( "我服,庫裏,杜蘭特,湯普森誰纔是出手的最佳選擇", 900),
  20. new News ( "易建聯的出場時間你能猜出來嗎,大概多少", 960),
  21. new News ( "卡戴珊三姐妹睡多少男人", 960),
  22. new News( "科比退役後湖人到底失去多少中國的粉絲", 4986),
  23. new News( "科比退役湖人籤中國籃球當家背後隱藏了多少陰謀", 65987)
  24. };
  25. adapter = new NewsAdapter(data, this);
  26. lv_test = FindViewById<ListView>(Resource.Id.lv_test);
  27. //View lv_header = LayoutInflater.Inflate(Resource.Layout.lv_header, null);
  28. //lv_test.AddHeaderView(lv_header);
  29. lv_test.Adapter = adapter;
  30. lv_test.ItemClick += (s, e) =>
  31. {
  32. OnClick(e.Position);
  33. };
  34. }
  35. public void OnClick(int position)
  36. {
  37. position--;
  38. Toast.MakeText( this, $"這條新聞有"+data[position].Pv+ "次瀏覽量",ToastLength.Short).Show();
  39. }
  40. }
  41. }


一個最簡單的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例子源碼下載

有興趣的能夠關注一下個人微信公衆號,分享一些編程相關的經典文章

股神獄中曝出莊家洗盤規律,牢記這3點,看完煥然大悟 南針信息 · 頂新
  • qq_33182090
    文佳銘: 在MainActivity.cs文件中,第28行代碼最後的Resource.Id.lv_test這是綁定listview控件,但博客上只定義了listview但在xaml中未出現過 (1年前 #3樓) 舉報回覆
  • 上一頁
  • 1
  • 下一頁
重磅!近期又出一股市怪才!50萬資金入市,現在身家過億! 南針信息 · 頂新
股神獄中曝出莊家洗盤規律,牢記這3點,A股就是提款機 南針信息 · 頂新
揭祕:靜脈曲張竟是身體缺了它?飯後吃點它,靜脈曲張再也不來 京宛協同 · 獵媒
老中醫:有多少人知道,原來它纔是痔瘡的剋星! 九龍 · 獵媒
如何知足她?老中醫說;教你一招,讓女人雙腿發抖! 同舟 · 獵媒
股市大消息,中國版納斯達克來了,對A股意味着什麼? 龍週刊 · 頂新
股神獄中曝出莊家洗盤規律,牢記這3點,看完煥然大悟 南針信息 · 頂新

沒有更多推薦了,返回首頁

相關文章
相關標籤/搜索