CardView Xamarin Android
本文关键字:Android Xamarin CardView | 更新日期: 2023-09-27 18:12:06
我试着让CardView
我有这个适配器的代码:
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Android.Support.V7.Widget;
using Android.Views;
namespace NavDrawer.Adapters
{
public class RecyclerAdapter<T, V> : RecyclerView.Adapter where V : RecyclerView.ViewHolder
{
ObservableCollection<T> _Collection;
int _ResourceId;
Action<T, V> _UpdateViewHolder;
public RecyclerAdapter(ObservableCollection<T> collection, int resourceId, Action<T, V> viewHolderUpdateAction)
{
_ResourceId = resourceId;
_Collection = collection;
_Collection.CollectionChanged += OnCollectionChanged;
_UpdateViewHolder = viewHolderUpdateAction;
}
~RecyclerAdapter()
{
_Collection.CollectionChanged -= OnCollectionChanged;
}
public override int ItemCount { get { return _Collection.Count; } }
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var viewHolder = holder as V;
var item = _Collection[position];
_UpdateViewHolder(item, viewHolder);
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(_ResourceId, parent, false);
return (V)Activator.CreateInstance(typeof(V), new object[] { itemView });
}
public T GetItem(int index)
{
return ((_Collection != null && index < _Collection.Count) ? _Collection[index] : default(T));
}
public void RemoveItem(int index)
{
if (_Collection != null && index < _Collection.Count) { _Collection.RemoveAt(index); }
}
public void InsertItemAt(int index, T item)
{
if (_Collection != null && index < _Collection.Count) { _Collection.Insert(index, item); }
}
public new Type GetType()
{
return typeof(T);
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
NotifyItemInserted(e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Remove:
NotifyItemRemoved(e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Replace:
NotifyItemChanged(e.OldStartingIndex);
NotifyItemChanged(e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Move:
NotifyItemRemoved(e.OldStartingIndex);
NotifyItemRemoved(e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Reset:
NotifyDataSetChanged();
break;
}
}
}
}
This to ViewHolder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.Widget;
namespace NavDrawer.Adapters
{
public class SponsorsViewHolder : RecyclerView.ViewHolder
{
public ImageView Image { get; private set; }
public TextView Sponsor { get; private set; }
public TextView Sponsordescription { get; private set; }
public SponsorsViewHolder(View itemView, Action<int> listener)
: base(itemView)
{
Image = itemView.FindViewById<ImageView>(Resource.Id.sponsorimage);
Sponsor = itemView.FindViewById<TextView>(Resource.Id.sponsorstitle);
Sponsordescription = itemView.FindViewById<TextView>(Resource.Id.sponsorsdescription);
}
/*void OnButtonClick(object sender, EventArgs e)
{
if (SomeAction != null)
{
SomeAction();
}*/
}
}
这里是我需要显示CardView的Fragment
using Android.App;
using Android.Content;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using NavDrawer.Activities;
using Fragment = Android.Support.V4.App.Fragment;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
using Com.Nostra13.Universalimageloader.Core;
using MvvmCross.Droid.Support.V7.Fragging.Fragments;
using MvvmCross.Droid.Support.V7.Fragging.Attributes;
using NavDrawer.Core.ViewModels;
using Android.Runtime;
using NavDrawer.DownloadClasses;
using System.Collections.Generic;
using static NavDrawer.Activities.FirstView;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System;
using Android.Support.V7.Widget;
using NavDrawer.Adapters;
namespace NavDrawer.Fragments
{
[MvxFragment(typeof(HomeViewModel), Resource.Id.content_frame)]
[Register("navdrawer.fragments.SponsorsFragment")]
public class SponsorsFragment : MvxFragment<SponsorsViewModel>
{
public class Sponsors
{
public string id { get; set; }
public string company_name { get; set; }
public string description { get; set; }
public string sponsor_level { get; set; }
public string picture { get; set; }
public string location { get; set; }
public string website_url { get; set; }
public string sm_twitter { get; set; }
public string sm_facebook { get; set; }
public string sm_linkedin { get; set; }
public string sm_pinterest { get; set; }
public string contact_number { get; set; }
public string attachments { get; set; }
public string date_time { get; set; }
}
public SponsorsFragment()
{
this.RetainInstance = true;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.SponsorsList, null);
GetAllSponsor sponsorlist = new GetAllSponsor();
sponsorlist.getallsponsor();
string spnsrlst = sponsorlist.ToFormattedJsonString();
List<Sponsors> sponsorObjectData = JsonConvert.DeserializeObject<List<Sponsors>>(spnsrlst);
recyclerView.SetLayoutManager(new LinearLayoutManager(Activity));
recyclerView.SetAdapter(new RecyclerAdapter<Sponsors, SponsorsViewHolder>(Spnsr, Resource.Layout.SponsorsCard));
return view;
}
}
}
我有一些问题:
- 我无法通过此代码
var recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
找到recyclerview - 我需要如何编写代码传递json数据到ObservableCollection?
有人能帮我吗?
谢谢
mate.
在你的SponsorsList布局中,你有RecyclerView视图吗?
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
我不太明白你第二个问题的意思,但这不是你想要的吗?recyclerView.SetAdapter(new RecyclerAdapter<Sponsors, SponsorsViewHolder>(sponsorObjectData, Resource.Layout.SponsorsCard));
我用sponsorObjectData代替了Spnsr。
我建议你阅读以下内容:https://developer.xamarin.com/guides/android/user_interface/recyclerview/和https://www.linkedin.com/pulse/creating-music-app-using-xamarin-sitecore-part-3-jo%C3%A3o-amancio-neto?trk=mp-author-card