我在片段中为recyclelerView编写了此代码RecyclerAdapter。我试图通过将一组颜色字符串传递给holder.cardView.setCardBackgroundColor(Color.parseColor())
来随机更改recyclerView中卡片的背景颜色。此代码工作且颜色随机变化但几秒后应用程序崩溃并在logcat中抛出以下错误< / p>
Skipped 49 frames! The application may be doing too much work on its main thread.
这是 RecyclerAdapter.java
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String[] postText;
String[] groupName;
String[] backColor = {"#ff6347", "#c2571a", "#a3b86c", "#3c6478", "#EBC94"};
public RecyclerAdapter(String[] postText, String[] groupName){
this.postText = postText;
this.groupName = groupName;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.txPost.setText(postText[position]);
holder.txGroupName.setText(groupName[position]);
holder.cardView.setCardBackgroundColor(Color.parseColor(backColor[position]));
}
@Override
public int getItemCount() {
return postText.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView txPost;
TextView txGroupName;
CardView cardView;
public RecyclerViewHolder(View view)
{
super(view);
txPost = (TextView) view.findViewById(R.id.post_text);
cardView = (CardView) itemView.findViewById(R.id.card_view);
txGroupName = (TextView) view.findViewById(R.id.group_name);
}
}
}
为什么会出现此错误?我该如何解决呢?一点点解释都会有所帮助。
答案 0 :(得分:-1)
我唯一能想到的是Color.parseColor
可能不必要地花费太长时间才能找到颜色代码中的颜色。尝试在创建适配器时解析所有颜色,看看它是否有效。