Recycler view showing a single item

I got a weird error when recyclerview shows only one item. Below is the code for the recycliewiew adapter:

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(); }} 

I already used this code in another application and it works fine. I also checked the chat data, which is also perfect.

Link to git repo layout files : layout files

+52
android android-recyclerview recycler-adapter
Apr 12 '16 at 2:35 pm
source share
2 answers

Do not use match_parent for height to view your element. One element fills the entire screen vertically, so you do not see another.

+208
Apr 12 '16 at 15:49
source share

when you create row.xml for recyclerview, you should follow these instructions:

  • Always use the "Wrap_content" parameter for line height, otherwise in "match_parent" it will occupy the entire screen for one line.

  • You can also take the height in dp.

+5
Feb 13 '17 at 1:31 on
source share



All Articles