• XamarinAndroid_BaseAdapter 简单的复用


    Xamarin-Android_BaseAdapter 简单的复用

    缘由:

      本人是一枚 小菜 初学Xamarin-Android  正在学习ListView 控件 发现这个控件的自定义布局 用的那叫一个爽字了得。

    但是每次用ListView的时候都要 继承一下BaseAdapter 这就不爽了。好气啊,我这个人非常的懒只好寻求改良之法。

      写这篇博客在这里献丑甚是尴尬啊,还请园子里的诸位大咖多多指正。小弟不胜感激!【谢谢O(∩_∩)O哈!】

    小弟学识浅薄仅仅做了以下的改良之处

    • 抽取泛型 因为基本上的是大同小异的 只是 类型的不同 如这次是 Person 类型 下次可能就是 Student 类型的区别而已 所以就想到了泛型
    • 将对 View 进行赋值的方法 使用委托进行替换。因为除了类型不同之外 也就这点不同了

    下面是效果图 自己都感觉丑 丑哭了 (⊙o⊙)…


    代码奉上

     ListItem的布局代码

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="horizontal"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:minWidth="25px"
     7     android:minHeight="25px">
     8     <ImageView
     9         android:layout_width="89.5dp"
    10         android:layout_height="67.5dp"
    11         android:id="@+id/imgHard" />
    12     <LinearLayout
    13         android:orientation="vertical"
    14         android:minWidth="25px"
    15         android:minHeight="25px"
    16         android:layout_width="200dp"
    17         android:layout_height="67.5dp"
    18         android:id="@+id/linearLayout1">
    19         <TextView
    20             android:text="Large Text"
    21             android:textAppearance="?android:attr/textAppearanceLarge"
    22             android:layout_width="279.5dp"
    23             android:layout_height="wrap_content"
    24             android:id="@+id/txtName" />
    25         <TextView
    26             android:text="Small Text"
    27             android:textAppearance="?android:attr/textAppearanceSmall"
    28             android:layout_width="match_parent"
    29             android:layout_height="wrap_content"
    30             android:id="@+id/txtDesc"
    31             android:textSize="12dp" />
    32     </LinearLayout>
    33     <Button
    34         android:text="查看信息"
    35         android:layout_width="wrap_content"
    36         android:layout_height="67.5dp"
    37         android:id="@+id/button1" />
    38 </LinearLayout>

    主界面的代码

     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     <ListView
     7         android:minWidth="25px"
     8         android:minHeight="25px"
     9         android:layout_width="match_parent"
    10         android:layout_height="match_parent"
    11         android:id="@+id/itemsPerson"
    12         android:divider="@android:color/white"
    13         android:dividerHeight="3dp"
    14         android:descendantFocusability="afterDescendants" />
    15 </LinearLayout>

    AllRoundBaseAdapter代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 using Android.App;
     7 using Android.Content;
     8 using Android.OS;
     9 using Android.Runtime;
    10 using Android.Views;
    11 using Android.Widget;
    12 
    13 namespace AdapterStudy_20170301
    14 {
    15     public class AllRoundAdapter<T> : BaseAdapter<T>
    16     {
    17         #region 全局变量
    18         /// <summary>
    19         /// 上下文
    20         /// </summary>
    21         public Activity MyContext { get; set; }
    22 
    23         /// <summary>
    24         /// 数据源
    25         /// </summary>
    26         public List<T> MyDataSource { get; set; }
    27 
    28         /// <summary>
    29         /// Item 布局资源
    30         /// </summary>
    31         public int LayoutSource { get; set; }
    32 
    33         /// <summary>
    34         /// 执行的委托
    35         /// </summary>
    36         Action<View, T> MyAction;
    37         #endregion
    38 
    39         /// <summary>
    40         /// 建立 AllRoundBaseAdapter
    41         /// </summary>
    42         /// <param name="actity">上下文</param>
    43         /// <param name="listDataSource">数据源</param>
    44         /// <param name="layoutSource">布局ID</param>
    45         /// <param name="action">为布局赋值用到的方法</param>
    46         public AllRoundAdapter(Activity actity, List<T> listDataSource, int layoutSource, Action<View, T> action)
    47         {
    48             this.MyContext = actity;
    49             this.MyDataSource = listDataSource;
    50 
    51             this.LayoutSource = layoutSource;
    52 
    53             this.MyAction = action;
    54         }
    55 
    56         public override T this[int position]
    57         {
    58             get
    59             {
    60                 return MyDataSource[position];
    61             }
    62         }
    63 
    64         public override int Count
    65         {
    66             get
    67             {
    68                 return this.MyDataSource.Count;
    69             }
    70         }
    71 
    72         public override long GetItemId(int position)
    73         {
    74             return position;
    75         }
    76 
    77         public override View GetView(int position, View convertView, ViewGroup parent)
    78         {
    79             T t = this.MyDataSource[position];
    80 
    81             View v = convertView;
    82             if (v == null)
    83             {
    84                 v = this.MyContext.LayoutInflater.Inflate(this.LayoutSource, null);
    85             }
    86 
    87             if (this.MyAction != null)
    88             {
    89                 this.MyAction.Invoke(v, t);
    90             }
    91 
    92             return v;
    93         }
    94     }
    95 }

    MainActivity的代码

     1 using System;
     2 using Android.App;
     3 using Android.Content;
     4 using Android.Runtime;
     5 using Android.Views;
     6 using Android.Widget;
     7 using Android.OS;
     8 using System.Collections.Generic;
     9 using Javax.Crypto;
    10 
    11 namespace AdapterStudy_20170301
    12 {
    13     [Activity(Label = "AdapterStudy_20170301", MainLauncher = true, Icon = "@drawable/icon")]
    14     public class MainActivity : Activity
    15     {
    16 
    17         private ListView lvPerson;
    18         private List<Person> listPerson = new List<Person>();
    19 
    20         protected override void OnCreate(Bundle bundle)
    21         {
    22             base.OnCreate(bundle);
    23 
    24             // Set our view from the "main" layout resource
    25             SetContentView(Resource.Layout.Main);
    26 
    27 
    28             lvPerson = FindViewById<ListView>(Resource.Id.itemsPerson);
    29 
    30             //填充一些数据
    31             this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
    32             this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
    33             this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
    34             this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
    35             this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
    36             this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
    37             this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
    38             this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
    39             this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
    40             this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
    41             this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
    42             this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
    43 
    44             //设置表头表尾
    45             View header = this.LayoutInflater.Inflate(Resource.Layout.HeaderLayout, null);
    46             View footer = this.LayoutInflater.Inflate(Resource.Layout.FooterLayout, null);
    47 
    48             this.lvPerson.AddHeaderView(header);
    49             this.lvPerson.AddFooterView(footer);
    50 
    51             //使用泛型的Adapter 进行赋值
    52             lvPerson.Adapter = new AllRoundAdapter<Person>(this, listPerson, Resource.Layout.ListViewItemLayout,
    53                 (x, y) =>
    54                 {
    55                     x.FindViewById<TextView>(Resource.Id.txtName).Text = y.Name;
    56                     x.FindViewById<TextView>(Resource.Id.txtDesc).Text = y.Desc;
    57                     x.FindViewById<ImageView>(Resource.Id.imgHard).SetBackgroundResource(y.Image);
    58                 });
    59 
    60         }
    61     }
    62 }

    学识浅薄,请多多指正,多多关照! 感觉自己好菜 掩面而逃。

  • 相关阅读:
    【android】手机增加一个新的实体按键
    python自动化错误解决方法
    java.lang.IllegalArgumentException: Buffer size too small. size = 262144 needed = 2205991
    50个开发工具
    mysql 查询json串
    软考题目单选题目
    软考操作系统基础知识
    使用DM将mysql具体表同步到tidb
    python日期处理
    安装部署dm
  • 原文地址:https://www.cnblogs.com/slyfox/p/6490865.html
Copyright © 2020-2023  润新知