我正在使用recyclerView,我成功地扩展了两个视图,但每个视图的内容都来自不同的json数据类型。我尝试在适配器中传递这两个数据类型,但它们没有正确绑定
>
源代码
公共类SimpleStringRecycleWebAdapter:RecycleWebView。适配器{
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;
}
}
}
公共类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;
}
}公共类SimpleViewHolder2:RecyclerView。视图持有者{public string mBoundString;public readonly View mView;public readonly ImageView mImageView;public readonly TextView mTxtView;public readonly TextView mTxtView 2;
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);
}
制作一个接口
来实现数据pojo类,如下所示。。
public interface Parent{
}
然后pojo类喜欢
public class User implements Parent{
private String name,addres;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddres() {
return addres;
}
public void setAddres(String addres) {
this.addres = addres;
}
}
然后将所有数据添加到父列表并绑定到适配器中。。适配器根据pojo类绑定数据,如下所示。。
public class SearchListAdapter extends ArrayAdapter<Parent> {
Context context;
Parent parent[] = null;
public SearchListAdapter(Context context, int layoutResourceId, Parent[] parent) {
super(context, layoutResourceId, parent);
this.context = context;
this.parent = parent;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
final View outerContainer = inflater.inflate(R.layout.food_list_item, parent, false);
if (this.parent != null) {
Parent temp = this.parent[position];
if (temp instanceof FoodItem) {
final FoodItem item = (FoodItem) temp;
}
}
}
}
使用此函数合并数据并在适配器中使用:
public List<Data> merge(Articel[] articles, List<YouTubeItem> items) {
int counter = 0,counter1=0,size=0;
size = articles.length + items.size();
List<Data> datas = new ArrayList<>();
for (int i = 0; i < size;i++) {
if (i % 2 == 0) {
if(counter1<articles.length){
Article article = articles[counter1];
Data data = new Data();
data.article = article;
data.youTubeItem = null;
data.type = 1;
datas.add(data);
counter1++;
}else if(counter<items.size()){
YouTubeItem item = items.get(counter);
Data data = new Data();
data.article = null;
data.youTubeItem = item;
data.type = 2;
datas.add(data);
counter++;
}
} else {
if(counter<items.size()){
YouTubeItem item = items.get(counter);
Data data = new Data();
data.article = null;
data.youTubeItem = item;
data.type = 2;
datas.add(data);
counter++;
}else if(counter1<articles.length){
Article article = articles[counter1];
Data data = new Data();
data.article = article;
data.youTubeItem = null;
data.type = 1;
datas.add(data);
counter1++;
}
}
}
return datas;
}
您应该只将数据合并到一个数据源。您可以这样尝试:
>
创建数据源类
public class Data {
int type; // 1 is article and 2 is youtubeitem
public Article article;
public YouTubeItem youTubeItem;
}
现在将两个数据源合并为一个
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;
}
更改适配器的构造函数
private List<Data> datas;
public SimpleStringRecyclerViewAdapter(Context context, List<Data> datas )
{
this.datas = datas;
}
更改获取项目计数
public override int ItemCount
{
get
{
return datas.Count();
}
}
更改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;
}
希望能有所帮助
问题内容: 我可以用来存储多种不同类型的数据(整数/字符串/等)?我来自PHP背景,可以将不同类型的数据存储到数组中,但是我不知道如何在Java中进行操作。 举个例子: 如何用Java做类似的事情? 问题答案: Java是一种强类型语言。在PHP或Javascript中,变量没有严格的类型。但是,在Java中,每个对象和基元都有严格的类型。您可以将多种类型的数据存储在Array中,但只能将其作为对
从使用RecycerView创建动态列表: 当我们创建时,我们必须指定将与适配器绑定的。 是否可以创建具有多种视图类型的?
我在我的应用程序中使用了具有多个视图的回收器视图。这工作正常,但我有一个问题,滚动这个回收视图!当我想上下滚动时,它有滞后现象。这是我的回收器视图适配器: 这个回收器视图有6种以上的视图类型,我必须显示,但是当回收器视图中的项目增加时,我在上下滚动时会有很大的延迟。我不知道为什么我在滚动上有延迟!我真的很感谢你的帮助。
如何设置文本颜色,我将使用setForebackground(color c),但如果我使用它,图像不会被打乱。 有可能使它与JLabel一起工作吗?在修改单元格的线程中设置所需的Jlabel(带有图像或文本),只需设置Valueat(label,row,column);
问题内容: 我有项目清单。一个项目可以是很多东西,比如说列表是这样的: 现在,我想使用ngRepeat指令渲染列表,该指令将根据对象的类型使用模板(多态渲染)。可以做到吗? 可能类似( ng-use 是一个 假设 指令): 问题答案: API参考:http://docs.angularjs.org/api/ng.directive: ngSwitch
问题内容: 很抱歉,如果这是重复的,尽管我无法在任何地方找到确切的答案: 有没有办法在 postgreSQL中 创建一个包含多个数据类型的数组? 我有一个类型的列(类型文本数组);尽管我想从type插入三个条目,然后插入第四个条目到此数组中。 有办法吗?如果是这样,怎么办? 问题答案: 我不认为有一种方法可以声明具有多个类型的数组。但是,我认为您可以使用复合类型来完成您想做的事情,例如, 然后,您