我创建了RecyclerViewAdapter,它通过int设置cardview背景,但是当我滚动一个大的列表时,颜色在不同的项目之间变化。
这是我的代码:
public class __MESSAGE_ListAdapter extends RecyclerView.Adapter<__MESSAGE_ListAdapter.ViewHolder>{
private Context context;
private List<__MESSAGE_Item> MessageItems;
AdapterViewCompat.OnItemClickListener mItemClickListener;
public class ViewHolder extends RecyclerView.ViewHolder{
// each data item is just a string in this case
public TextView title,writer,time,message,index, parent;
public CardView cardView;
public ViewHolder(View v) {
super(v);
title = (TextView) itemView.findViewById(R.id.messageTitle);
time = (TextView) itemView.findViewById(R.id.messageTime);
writer = (TextView) itemView.findViewById(R.id.messageWriter);
message = (TextView) itemView.findViewById(R.id.messageContent);
index = (TextView) itemView.findViewById(R.id.messageIndex);
parent = (TextView) itemView.findViewById(R.id.messageParent);
cardView = (CardView) itemView.findViewById(R.id.card_view);
}
}
public void add(int position, __MESSAGE_Item item) {
MessageItems.add(position, item);
notifyItemInserted(position);
}
public void remove(__MESSAGE_Item name) {
int position = MessageItems.indexOf(name);
MessageItems.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public __MESSAGE_ListAdapter(List<__MESSAGE_Item> myDataset, Context context) {
MessageItems = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public __MESSAGE_ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
this.context = parent.getContext();
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final __MESSAGE_Item name = MessageItems.get(position);
if (Integer.parseInt(MessageItems.get(position).getIndex()) != 0)
{
holder.title.setText(MessageItems.get(position).getIndex() + "." + MessageItems.get(position).getTitle());
holder.parent.setText(MessageItems.get(position).getParent());
}
else {
holder.title.setText(MessageItems.get(position).getTitle());
holder.parent.setText("");
holder.parent.setVisibility(View.GONE);
}
//Set writer
holder.writer.setText(MessageItems.get(position).getWriter());
//Timestamp to Date
holder.time.setText(new SimpleDateFormat("hh:mm dd/MM/yyyy")
.format(new Date(MessageItems.get(position).getTime() * 1000L)));
//Check if the message is empty - not work!
if(MessageItems.get(position).getMessage() != "")
{
UrlImageParser p = new UrlImageParser(holder.message, context);
Spanned htmlSpan = Html.fromHtml(MessageItems.get(position).getMessage(), p, null);
holder.message.setText(htmlSpan);
//Make links
Linkify.addLinks(holder.message, Linkify.WEB_URLS);
}
else {
holder.message.setVisibility(View.GONE);
}
//Set index
holder.index.setText(MessageItems.get(position).getIndex());
//Set colour by level
int messageLevel = MessageItems.get(position).getLevel();
if (messageLevel >= 2)
{
if (messageLevel == 2)
{
holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.level_1));
}
else if(messageLevel == 3)
{
holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.level_2));
}
else if(messageLevel == 4)
{
holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.level_3));
}
else if(messageLevel == 5)
{
holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.level_4));
}
else
{
holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.level_5));
}
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return MessageItems.size();
}
}
知道怎么解决吗?
我看到了关于listview适配器的这个问题,但问题不在这里。
有关convertView == null
的问题,但这段代码在哪里?