I am working with a recyclerView and i succeeded in inflating two views but each view content comes from different json data types. i tried passing the two datatypes in the adapter but they are not properly binded
source code
public class SimpleStringRecyclerViewAdapter : RecyclerView.Adapter {
private Article[] mValues; private List<YouTubeItem> mValues2; Context context; public SimpleStringRecyclerViewAdapter(Context context, Article[] items, List<YouTubeItem> item ) { this.context = context; mValues = items; mValues2 = item; } public override int ItemCount { get { return mValues.Count() + mValues2.Count(); } } public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder is SimpleViewHolder) try { Article item = mValues[position]; var simpleHolder = holder as SimpleViewHolder; simpleHolder.mTxtView.Text = Android.Text.Html.FromHtml(item.Title).ToString(); simpleHolder.mTxtView2.Text = item.Description; using (var imageView = simpleHolder.mImageView) { string url = Android.Text.Html.FromHtml(item.UrlToImage).ToString(); //Download and display image UrlImageViewHelper.SetUrlDrawable(imageView, url, Resource.Drawable.cheese_1 ); } // simpleHolder.mprogressbar.Visibility = ViewStates.Gone; } catch (Exception e) { //Toast.MakeText(this.context, e.ToString(), ToastLength.Long).Show(); } else { try { YouTubeItem item = mValues2[position]; var simpleHolder = holder as SimpleViewHolder2; simpleHolder.mTxtView.Text = Android.Text.Html.FromHtml(item.Title).ToString(); // simpleHolder.mTxtView2.Text = item.DescriptionShort; using (var imageView = simpleHolder.mImageView) { string url = Android.Text.Html.FromHtml(item.MaxResThumbnailUrl).ToString(); //Download and display image UrlImageViewHelper.SetUrlDrawable(imageView, url, Resource.Drawable.cheese_1 ); } } catch (Exception e) { //Toast.MakeText(this.context, e.ToString(), ToastLength.Long).Show(); } } } public override int GetItemViewType(int position) { if ((position % 2) == 0) { //Even number return Resource.Layout.List_Item; } else { //Odd number return Resource.Layout.VideoList; } } public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == Resource.Layout.List_Item) { View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_Item, parent, false); view.SetBackgroundColor(Color.White); SimpleViewHolder holder = new SimpleViewHolder(view); // holder.mprogressbar = view.FindViewById<ProgressBar>(Resource.Id.progressBar); // holder.mprogressbar.Visibility = ViewStates.Visible; //Showing loading progressbar return holder; } else { View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.VideoList, parent, false); view.SetBackgroundColor(Color.White); SimpleViewHolder2 holder = new SimpleViewHolder2(view); return holder; } }
}
public class SimpleViewHolder : RecyclerView.ViewHolder { public string mBoundString; public readonly View mView; public readonly ImageView mImageView; public readonly TextView mTxtView; public readonly TextView mTxtView2; // public ProgressBar mprogressbar;
public SimpleViewHolder(View view) : base(view) { mView = view; mImageView = view.FindViewById<ImageView>(Resource.Id.avatar); mTxtView = view.FindViewById<TextView>(Resource.Id.Text1); mTxtView2 = view.FindViewById<TextView>(Resource.Id.Text2); // mprogressbar = view.FindViewById<ProgressBar>(Resource.Id.progressBar); } public override string ToString() { return base.ToString() + " '" + mTxtView.Text; }
} public class SimpleViewHolder2 : RecyclerView.ViewHolder { public string mBoundString; public readonly View mView; public readonly ImageView mImageView; public readonly TextView mTxtView; public readonly TextView mTxtView2;
public SimpleViewHolder2(View view) : base(view) { mView = view; mImageView = view.FindViewById<ImageView>(Resource.Id.videoavatar); mTxtView = view.FindViewById<TextView>(Resource.Id.videoText1); // mprogressbar = view.FindViewById<ProgressBar>(Resource.Id.progressBar); }
use this function to merge the data and use this in your adapter :
You should merge data to one data source only. You can try this way:
Create data source class
Now merge two data source to only one
Change constructor of adapter
Change get Item count
Change getViewType
EDITED: For merge random method
For merge odd&even method
Hope it help
Make one
interface
to implement your data pojo class like below..Then pojo class like
Then add all the data into parent list and bind into adapter .. Adapter bind data according to pojo class like below..