RecyclerView未正确显示消息列表

时间:2018-03-19 18:42:52

标签: android android-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;
    }
}

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。

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

答案 1 :(得分:0)

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

mAdapter.notifyDataSetChanged();

当你想加载新邮件时