gpt4 book ai didi

android - RecyclerView 具有多种 View 类型和数据源

转载 作者:行者123 更新时间:2023-12-02 02:07:49 25 4
gpt4 key购买 nike

我正在使用 recyclerView,并且成功地膨胀了两个 View ,但每个 View 内容都来自不同的 json 数据类型。我尝试在适配器中传递两种数据类型,但它们没有正确绑定(bind)

  • 源代码

    公共(public)类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)类SimpleViewHolder:RecyclerView.ViewHolder{ 公共(public)字符串 mBoundString; 公共(public)只读 View mView; 公共(public)只读ImageView mImageView; 公共(public)只读 TextView mTxtView; 公共(public)只读 TextView mTxtView2; //公共(public)进度条 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)类 SimpleViewHolder2 :RecyclerView.ViewHolder{ 公共(public)字符串 mBoundString; 公共(public)只读 View mView; 公共(public)只读ImageView mImageView; 公共(public)只读 TextView mTxtView; 公共(public)只读 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);


    }

最佳答案

您应该仅将数据合并到一个数据源。您可以尝试以下方法:

  1. 创建数据源类

    public class Data {
    int type; // 1 is article and 2 is youtubeitem
    public Article article;
    public YouTubeItem youTubeItem;
    }
  2. 现在将两个数据源合并为一个

    public List<Data> merge(Articel[] articles, List<YouTubeItem> items) {
    List<Data> datas = new ArrayList<>();
    for(Article article : articles) {
    Data data = new Data();
    data.article = article;
    data.youTubeItem = null;
    data.type = 1;
    datas.add(data);
    }

    for(YouTubeItem item : items) {
    Data data = new Data();
    data.article = null;
    data.youTubeItem = item;
    data.type = 2;
    datas.add(data);
    }

    return datas;
    }
  3. 更改适配器的构造函数

    private List<Data> datas;

    public SimpleStringRecyclerViewAdapter(Context context, List<Data> datas )
    {
    this.datas = datas;
    }
  4. 更改获取项目计数

    public override int ItemCount
    {

    get
    {
    return datas.Count();
    }
    }
  5. 更改 getViewType

    public override int GetItemViewType(int position)
    {
    if (datas.get(position).type == 1)
    {
    return Resource.Layout.List_Item;
    }

    else
    {
    return Resource.Layout.VideoList;
    }
    }

编辑:用于合并随机方法

 public List<Data> mergeRandom(Articel[] articles, List<YouTubeItem> items) {
List<Data> datas = new ArrayList<>();

List<Integer> random = new ArrayList<>();
int maxLength = articles.length + items.size();
for(int i = 0; i< maxLength; i++) {
random.add(i);
}

while (random.size() > 0) {
// get random item
int index = new Random().nextInt(random.size());
int position = random.get(index);

if(position <= article.length - 1) {
Data data = new Data();
data.article = articles[position];
data.youTubeItem = null;
data.type = 1;
datas.add(data);
} else {
Data data = new Data();
data.article = null;
data.youTubeItem = items.get(position - article.length);
data.type = 2;
datas.add(data);
}

random.remove(index);
}

return datas;
}

奇偶合并方法

List<Data> mergeOddEven(Articel[] articles, List<YouTubeItem> items) {
List<Data> datas = new ArrayList<>();

int articleIndex = 0;
int youtubeIndex = 0;
int length = articles.length + items.size();

for(int i = 0; i< length; i++) {
if(articleIndex >= articles.length || youtubeIndex >= items.size()) {
if(articleIndex < articles.length) {
for(int j = articleIndex; j < articles.length ; j++) {
Data data = new Data();
data.article = articles[j];
data.youTubeItem = null;
data.type = 1;
datas.add(data);

}
} else {
for(int j = youtubeIndex; j < items.size() ; j++) {
Data data = new Data();
data.article = null;
data.youTubeItem = items.get(j);
data.type = 2;
datas.add(data);

}
}

break;
}


if(i % 2 == 0) {
Data data = new Data();
data.article = articles[articleIndex];
data.youTubeItem = null;
data.type = 1;
datas.add(data);

articleIndex++;
} else {
Data data = new Data();
data.article = null;
data.youTubeItem = tems.get(youtubeIndex);
data.type = 2;
datas.add(data);

youtubeIndex++;
}
}

return datas;
}

希望对你有帮助

关于android - RecyclerView 具有多种 View 类型和数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49853500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com