我遇到一个奇怪的错误,其中recyclerview仅显示一个项目。以下是我的recyclerview适配器的代码:
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<chat> chats;
String username;
final int My=0,Their=1;
public ChatAdapter(List<chat> chats) {
this.chats = chats;
this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case My:
View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v1);
break;
case Their:
View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
viewHolder = new TheirMessageHolder(v2);
break;
default:
View v = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case My:
MyChatHolder myChatHolder = (MyChatHolder) holder;
myChatHolder.setMyMessage(chats.get(position).getMessage());
break;
case Their:
TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
break;
}
}
@Override
public int getItemViewType(int position) {
if(username.equalsIgnoreCase(chats.get(position).getFrom()))
return My;
return Their;
}
@Override
public int getItemCount() {
return chats.size();
}}
我已经在其他应用程序中使用了此代码,并且可以正常工作。我检查了聊天数据,这也是完美的。
这是git repo布局文件的链接: 布局文件
@OShiffer我添加了git repo链接
—
Rujul1993 '16
尝试设置
—
Slavik
wrap_content
,而不是match_parent
高度和宽度的布局github.com/revic1993/androidchat/blob/master/app/src/main/res/... github.com/revic1993/androidchat/blob/master/app/src/main / res /…
@Slavik谢谢!
—
Rujul1993 '16