RecyclerView未正确显示消息列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView未正确显示消息列表相关的知识,希望对你有一定的参考价值。

我正在实现聊天应用程序,我正在使用RecyclerView处理发件人和收件人邮件,除了我的聊天活动用户界面外,所有工作都正常,当发件人发送邮件然后通过向下滚动活动收到的邮件时,它看起来像发件人邮件在一个屏幕上,接收者消息在另一个屏幕上,当向下滚动屏幕时,该消息变得可见。

    messageList = findViewById(R.id.messageList);
    messageList.setHasFixedSize(true);

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    messageList.setLayoutManager(llm);
    messageList.setAdapter(mAdapter);

这里messageList是包含发送者和接收者消息的列表。

这是适配器代码

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final Context context;
private final ArrayList<Message> messages;

private static final int VIEW_HOLDER_TYPE_1 = 1;
private static final int VIEW_HOLDER_TYPE_2 = 2;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder

public static class ViewHolder_Type1 extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mymessageTextView, mytimeTextView;

    public ViewHolder_Type1(View v) {
        super(v);
        this.mymessageTextView = (TextView) v.findViewById(R.id.mymessageTextView);
        this.mytimeTextView = (TextView) v.findViewById(R.id.mytimeTextView);
    }
}

public static class ViewHolder_Type2 extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView messageTextView, timeTextView;
    public ViewHolder_Type2(View v) {
        super(v);
        this.messageTextView = (TextView) v.findViewById(R.id.messageTextView);
        this.timeTextView = (TextView) v.findViewById(R.id.timeTextView);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Context context, ArrayList<Message> messages) {
    this.context = context;
    this.messages = messages;
}

// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;
    switch (viewType) {
        // create a new view
        case VIEW_HOLDER_TYPE_1:
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mymessage, parent, false);
            ViewHolder_Type1 vh1 = new ViewHolder_Type1(v);
            return vh1;
        case VIEW_HOLDER_TYPE_2:
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.message, parent, false);
            ViewHolder_Type2 vh2 = new ViewHolder_Type2(v);
            return vh2;
        default:
            break;
    }
    return null;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    switch (getItemViewType(position)) {
        case VIEW_HOLDER_TYPE_1:
            ViewHolder_Type1 viewholder1 = (ViewHolder_Type1) holder;
            TextView mytimeView = (TextView) viewholder1.mytimeTextView;
            mytimeView.setText(messages.get(position).getTime());
            TextView mymsgView = (TextView) viewholder1.mymessageTextView;
            mymsgView.setText(messages.get(position).getMessage());
            break;
        case VIEW_HOLDER_TYPE_2:
            ViewHolder_Type2 viewholder2 = (ViewHolder_Type2) holder;
            TextView timeView = (TextView) viewholder2.timeTextView;
            timeView.setText(messages.get(position).getTime());
            TextView msgView = (TextView) viewholder2.messageTextView;
            msgView.setText(messages.get(position).getMessage());
            break;
        default:
            break;
    }
}
答案

我解决了这个问题。

我创建了两个资源文件一个发送者消息和一个接收者消息这些文件的根元素有match-parent宽度和高度,我只需用wrap-content替换现在一切都很好。

另一答案

我相信您需要在收到新消息时始终通知您的适配器,当您滚动时,请使用新信息重新绘制回收器。所以说

mAdapter.notifyDataSetChanged();

当您要加载新邮件时

以上是关于RecyclerView未正确显示消息列表的主要内容,如果未能解决你的问题,请参考以下文章

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

RecyclerView未显示已解析的GSON数据

从其他片段添加新的 RecyclerView 项

将对象列表从片段传递到 recyclerView 适配器

RecyclerView 内容未使用片段父级的全宽

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview