Android Xamarin中的自定义ListView

2020-03-26 c# android xamarin

我正在尝试在自定义ListView中显示数据。
从api提取的数据正在运行,但未在ListView中显示。

Answers

请尝试此步骤,以在Xamarin Android中自定义Listview:

步骤1:-在XML文件中创建ListView。

步骤2:-在MainActivity中初始化ListView。

public class MainActivity : Activity  
    {  
        ListView myList;  
        protected override void OnCreate(Bundle bundle)  
        {  
            base.OnCreate(bundle);  

            SetContentView(Resource.Layout.Main);  

            myList = FindViewById<ListView>(Resource.Id.listView);  

        }  
    }      

步骤3:-然后为ListView单元格创建一行,以将用户的详细信息显示为
名称,图像和部门。

步骤4:-建立使用者模型。

public class User  
    {  
        public string Name { get; set; }  
        public string ImageUrl { get; set; }  
        public string Department { get; set; }  
        public string Details { get; set; }  

        public override string ToString()  
        {  
            return Name + " " + Department;  
        }  
    }   

步骤5:-要绑定该行,我们需要ListView Adapter,因此创建一个Custom Adapter。

public class MyCustomListAdapter : BaseAdapter<User>  
    {  
        List<User> users;  

        public MyCustomListAdapter(List<User> users)  
        {  
            this.users = users;  
        }  

        public override User this[int position]  
        {  
            get  
            {  
                return users[position];  
            }  
        }  

        public override int Count  
        {  
            get  
            {  
                 return users.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 = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.userRow, parent, false);  

                var photo = view.FindViewById<ImageView>(Resource.Id.photoImageView);  
                var name = view.FindViewById<TextView>(Resource.Id.nameTextView);  
                var department = view.FindViewById<TextView>(Resource.Id.departmentTextView);  

                view.Tag = new ViewHolder() { Photo = photo, Name = name, Department = department };  
            }  

            var holder = (ViewHolder)view.Tag;  

            holder.Photo.SetImageDrawable(ImageManager.Get(parent.Context, users[position].ImageUrl));  
            holder.Name.Text = users[position].Name;  
            holder.Department.Text = users[position].Department;  


            return view;  

        } 
    }

步骤6:-创建图像管理器类,该类用于将图像与ImageView绑定。

public static class ImageManager  
    {  
        static Dictionary<string, Drawable> cache = new Dictionary<string, Drawable>();  

        public static Drawable Get(Context context, string url)  
        {  
            if (!cache.ContainsKey(url))  
            {  
                var drawable = Drawable.CreateFromStream(context.Assets.Open(url), null);  

                cache.Add(url, drawable);  
            }  

            return cache[url];  
        }  
    }   

步骤7:-创建UserData文件以将数据提供给列表。

public static class UserData  
    {  
        public static List<User> Users { get; private set; }  

        static UserData()  
        {  
            var temp = new List<User>();  

            AddUser(temp);  
            AddUser(temp);  
            AddUser(temp);  

            Users = temp.OrderBy(i => i.Name).ToList();  
        }  

        static void AddUser(List<User> users)  
        {  
            users.Add(new User()  
            {  
                Name = "aVirendra Thakur",  
                Department = "Xamarin Android & Xamarin Forms Development",  
                ImageUrl = "images/image.png",  
                Details = "Virendra has over 2 years of experience developing mobile applications,"               
            });  

            users.Add(new User()  
            {  
                Name = "bVirendra Thakur",  
                Department = "Xamarin Android & Xamarin Forms Development",  
                ImageUrl = "images/image2.png",  
                Details = "Virndra has over 2 years of experience developing mobile applications; specializing in Android & C# cross platform development."  

            });  
            users.Add(new User()  
            {  
                Name = "bVirendra Thakur",  
                Department = "Xamarin Android & Xamarin Forms Development",  
                ImageUrl = "images/image.png",  
                Details = "Virndra has over 2 years of experience developing mobile applications; specializing in Android & C# cross platform development."  

            });  

} 

步骤8:-创建ViewHolder类,其中包含视图的控​​件。它只会创建控件并在ListView中重用它,从而节省了内存。

public class ViewHolder: Java.Lang.Object  
    {  
        public ImageView Photo { get; set; }  
        public TextView Name { get; set; }  
        public TextView Department { get; set; }  
    }   

步骤9:-MyCustomListAdapter并添加几行代码以绑定数据。

public override View GetView(int position, View convertView, ViewGroup parent)  
        {  
            var view = convertView;  

            if(view==null)  
            {  
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.userRow, parent, false);  

                var photo = view.FindViewById<ImageView>(Resource.Id.photoImageView);  
                var name = view.FindViewById<TextView>(Resource.Id.nameTextView);  
                var department = view.FindViewById<TextView>(Resource.Id.departmentTextView);  

                view.Tag = new ViewHolder() { Photo = photo, Name = name, Department = department };  
            }  

            var holder = (ViewHolder)view.Tag;  

            holder.Photo.SetImageDrawable(ImageManager.Get(parent.Context, users[position].ImageUrl));  
            holder.Name.Text = users[position].Name;  
            holder.Department.Text = users[position].Department;  


            return view;  

        }  

步骤10:-将自定义适配器添加到MainActivity中的ListView。

protected override void OnCreate(Bundle bundle)  
        {  
            base.OnCreate(bundle);  

            SetContentView(Resource.Layout.Main);  

            myList = FindViewById<ListView>(Resource.Id.listView);  

            myList.Adapter = new MyCustomListAdapter(UserData.Users);  
        }   

我希望这一步骤会有用。

谢谢。

Related