我想在适配器中设置多行,但这不起作用,我不知道为什么
聊天适配器
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<ChatModel> chatModels;
public ChatAdapter(Context context, List<ChatModel> chatModels) {
this.context = context;
this.chatModels = chatModels;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ChatModel chatModel = chatModels.get(i);
if (chatModel.me.equals("yes")) {
return new Black_Message(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_black, viewGroup, false));
} else if (chatModel.me.equals("no")) {
return new Yellow_Message(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_yellow, viewGroup, false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ChatModel chatModel = chatModels.get(i);
if (chatModel.me.equals("yes")) {
Black_Message black_message = (Black_Message) viewHolder;
black_message.text_title.setText(chatModel.question);
} else if (chatModel.me.equals("no")) {
Yellow_Message yellow_message = (Yellow_Message) viewHolder;
yellow_message.text_title.setText(chatModel.answers);
}
}
@Override
public int getItemCount() {
return chatModels.size();
}
class Black_Message extends RecyclerView.ViewHolder {
@BindView(R.id.relative_layout)
RelativeLayout relative_layout;
@BindView(R.id.text_title)
TextViewFont text_title;
public Black_Message(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
class Yellow_Message extends RecyclerView.ViewHolder {
@BindView(R.id.relative_layout)
RelativeLayout relative_layout;
@BindView(R.id.text_title)
TextViewFont text_title;
public Yellow_Message(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
我希望你告诉我哪里出错了.......................................... ................................................... ................................................... >
答案 0 :(得分:0)
我找到了解决办法
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<ChatModel> chatModels;
public ChatAdapter(Context context, List<ChatModel> chatModels) {
this.context = context;
this.chatModels = chatModels;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == 1) {
return new Black_Message(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_black, viewGroup, false));
} else if (i == 2) {
return new Yellow_Message(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_yellow, viewGroup, false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ChatModel chatModel = chatModels.get(i);
if (chatModel.me.equals("yes")) {
Black_Message black_message = (Black_Message) viewHolder;
black_message.text_title.setText(chatModel.question);
} else if (chatModel.me.equals("no")) {
Yellow_Message yellow_message = (Yellow_Message) viewHolder;
yellow_message.text_title.setText(chatModel.answers);
}
}
@Override
public int getItemCount() {
return chatModels.size();
}
@Override
public int getItemViewType(int position) {
ChatModel chatModel = chatModels.get(position);
if (chatModel.me.equals("yes")) {
return 1;
} else if (chatModel.me.equals("no")) {
return 2;
}else {
return super.getItemViewType(position);
}
}
class Black_Message extends RecyclerView.ViewHolder {
@BindView(R.id.relative_layout)
RelativeLayout relative_layout;
@BindView(R.id.text_title)
TextViewFont text_title;
public Black_Message(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
class Yellow_Message extends RecyclerView.ViewHolder {
@BindView(R.id.relative_layout)
RelativeLayout relative_layout;
@BindView(R.id.text_title)
TextViewFont text_title;
public Yellow_Message(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}