我正在使用具有5种不同视图的recyclerview。但是当我尝试将任何数据绑定到我的视图时,它根本不会显示。 如果我没有绑定任何数据并只显示不同的视图,它显示正常。我错过了什么?
package com.example.chrisjohnson.icitizenv2.Adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chrisjohnson.icitizenv2.Card;
import com.example.chrisjohnson.icitizenv2.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by chrisjohnson on 7/7/15.
*/
public class TodayRecycleAdapter extends RecyclerView.Adapter<TodayRecycleAdapter.ViewHolder> {
private ArrayList<Card> data;
private Context context;
private static final int CASE_POLLS = 0;
private static final int CASE_REPS = 1;
private static final int CASE_NEWS = 2;
private static final int CASE_BILLS = 3;
private static final int CASE_PEERS = 4;
public TodayRecycleAdapter(ArrayList<Card> data, Context context) {
this.data = data;
this.context = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView type;
TextView title;
TextView text;
TextView office;
TextView location;
TextView time;
TextView district;
ImageView image;
ImageView rep_office_icon;
ImageView news_image;
public ViewHolder(View itemView) {
super(itemView);
switch (getItemViewType()) {
case 0:
type = (TextView) itemView.findViewById(R.id.card_type);
text = (TextView) itemView.findViewById(R.id.card_text);
time = (TextView) itemView.findViewById(R.id.card_poll_time);
image = (ImageView) itemView.findViewById(R.id.card_icon);
break;
case 1:
type = (TextView) itemView.findViewById(R.id.card_type);
text = (TextView) itemView.findViewById(R.id.card_text);
image = (ImageView) itemView.findViewById(R.id.card_icon);
news_image = (ImageView) itemView.findViewById(R.id.card_news_image);
break;
case 2:
type = (TextView) itemView.findViewById(R.id.card_type);
text = (TextView) itemView.findViewById(R.id.card_rep_name);
image = (ImageView) itemView.findViewById(R.id.card_rep_image);
office = (TextView) itemView.findViewById(R.id.card_rep_office);
location = (TextView) itemView.findViewById(R.id.card_rep_location);
break;
case 3:
type = (TextView) itemView.findViewById(R.id.card_type);
title = (TextView) itemView.findViewById(R.id.card_title);
text = (TextView) itemView.findViewById(R.id.card_text);
image = (ImageView) itemView.findViewById(R.id.card_bill_type_image);
break;
case 4:
type = (TextView) itemView.findViewById(R.id.card_type);
text = (TextView) itemView.findViewById(R.id.card_text);
time = (TextView) itemView.findViewById(R.id.card_suggested);
image = (ImageView) itemView.findViewById(R.id.card_icon);
location = (TextView) itemView.findViewById(R.id.card_peer_location);
district = (TextView) itemView.findViewById(R.id.card_peer_district);
break;
}
}
public ViewHolder(View itemView, TextView type, TextView title, TextView text,
TextView office, TextView location, TextView time, TextView district,
ImageView image, ImageView rep_office_icon, ImageView news_image) {
super(itemView);
this.type = type;
this.title = title;
this.text = text;
this.office = office;
this.location = location;
this.time = time;
this.district = district;
this.image = image;
this.rep_office_icon = rep_office_icon;
this.news_image = news_image;
}
}
@Override
public TodayRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = null;
switch (viewType) {
case 0:
v = LayoutInflater.from(context).inflate(R.layout.polls_card_layout, null);
break;
case 1:
v = LayoutInflater.from(context).inflate(R.layout.news_card_layout, null);
break;
case 2:
v = LayoutInflater.from(context).inflate(R.layout.reps_card_layout, null);
break;
case 3:
v = LayoutInflater.from(context).inflate(R.layout.bills_card_layout, null);
break;
case 4:
v = LayoutInflater.from(context).inflate(R.layout.peers_card_layout, null);
break;
}
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(TodayRecycleAdapter.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case CASE_POLLS:
viewHolder.type.setText(data.get(position).getType());
viewHolder.text.setText(data.get(position).getText());
viewHolder.time.setText(data.get(position).getTime());
break;
case CASE_NEWS:
viewHolder.type.setText(data.get(position).getType());
viewHolder.text.setText(data.get(position).getText());
Picasso.with(context).load(R.drawable.news_photo).placeholder(R.drawable.news_photoholder).into(viewHolder.news_image);
break;
case CASE_REPS:
viewHolder.type.setText(data.get(position).getType());
viewHolder.text.setText(data.get(position).getText());
viewHolder.office.setText(data.get(position).getOffice());
viewHolder.location.setText(data.get(position).getLocation());
/*if (position > 3) {
Picasso.with(context).load(R.drawable.rep_obama).placeholder(R.drawable.rep_obama).into(viewHolder.image);
}*/
break;
case CASE_BILLS:
viewHolder.type.setText(data.get(position).getType());
viewHolder.title.setText(data.get(position).getTitle());
viewHolder.text.setText(data.get(position).getText());
break;
case CASE_PEERS:
viewHolder.type.setText(data.get(position).getType());
viewHolder.text.setText(data.get(position).getText());
viewHolder.time.setText(data.get(position).getTime());
viewHolder.location.setText(data.get(position).getLocation());
viewHolder.district.setText(data.get(position).getDistrict());
break;
}
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public int getItemViewType(int position) {
if (data.get(position).getType().equals("Bills")) {
return CASE_BILLS;
}
else if (data.get(position).getType().equals("Reps")) {
return CASE_REPS;
}
else if (data.get(position).getType().equals("Polls")) {
return CASE_POLLS;
}
else if (data.get(position).getType().equals("Peers")) {
return CASE_PEERS;
}
else {
return CASE_NEWS;
}
}
}
答案 0 :(得分:0)
我明白了。问题出在ViewHolder的构造函数中。
我从来没有调用过getItemViewType的switch语句。所以现在我通过onCreateViewHolder方法找到了视图。可能有更好的方法来绑定视图,但我还没有找到它。