0
私はアダプタのためにこのコードを持ってCardViewCardView XamarinのAndroid
ようにしよう:ViewHolderへ
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;
}
}
}
}
この:
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:
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;
}
}
}
私はいくつかの質問があります。
- を私はのObservableCollectionにJSONデータを渡すために、コードを記述する必要がどのようにこのコード
var recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
- 経由recyclerviewを見つけるカント?
誰かが私を助けることができますか?
ありがとうございました