Recyclerview在使用不同的Viewholder时无法顺畅地折叠

问题描述 投票:1回答:1

我正在创建一个聊天应用程序,根据收到的消息类型,该应用程序将具有不同类型的布局。每个视图持有者都有自己的布局和实现。该应用程序工作正常,但在滚动时会滞后,尤其是在布局类型更改时。

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final int itemType = getItemViewType(position);
    switch (itemType) {
        case Const.CHAT_NOTIFICATION_TYPE_NOTIFY:
            ((NotifyViewHolder) holder).bindData(position);
            break;
        case VIEW_TYPE_DATE_HEADER:
            ((DefaultDateHeaderViewHolder) holder).bindData(position);
            break;
        case VIEW_TYPE_TEXT_MESSAGE:
            ((OutgoingViewHolder) holder).bindData(position);
            break;
        case -VIEW_TYPE_TEXT_MESSAGE:
            ((IncomingViewHolder) holder).bindData(position);
            break;
        case VIEW_TYPE_IMAGE_MESSAGE:
            ((OutgoingImageViewHolder) holder).bindData(position);
            break;
        case -VIEW_TYPE_IMAGE_MESSAGE:
            ((IncomingImageViewHolder) holder).bindData(position);
            break;
        case VIEW_TYPE_ATTACHMENT:
            ((OutgoingAttachmentViewHolder) holder).bindData(position);
            break;
        case -VIEW_TYPE_ATTACHMENT:
            ((IncomingAttachmentViewHolder) holder).bindData(position);
            break;
    }
    throw new IllegalStateException("Wrong message view type. Please, report this issue on GitHub with full stacktrace in description.");
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View row;
    switch (viewType) {
        case Const.CHAT_NOTIFICATION_TYPE_NOTIFY:
            row = LayoutInflater.from(context).inflate(R.layout.notify_bubble, parent, false);
            return new NotifyViewHolder(row);
        case VIEW_TYPE_DATE_HEADER:
            row = LayoutInflater.from(context).inflate(R.layout.item_date_header, parent, false);
            return new NotifyViewHolder(row);
        case VIEW_TYPE_TEXT_MESSAGE:
            row = LayoutInflater.from(context).inflate(R.layout.outgoing_bubble, parent, false);
            return new OutgoingViewHolder(row);
        case -VIEW_TYPE_TEXT_MESSAGE:
            row = LayoutInflater.from(context).inflate(R.layout.incoming_bubble, parent, false);
            return new IncomingViewHolder(row);
        case VIEW_TYPE_IMAGE_MESSAGE:
            row = LayoutInflater.from(context).inflate(R.layout.item_outcoming_image_message, parent, false);
            return new OutgoingImageViewHolder(row);
        case -VIEW_TYPE_IMAGE_MESSAGE:
            row = LayoutInflater.from(context).inflate(R.layout.item_incoming_image_message, parent, false);
            return new IncomingImageViewHolder(row);
        case VIEW_TYPE_ATTACHMENT:
            row = LayoutInflater.from(context).inflate(R.layout.item_outcoming_attachment, parent, false);
            return new OutgoingAttachmentViewHolder(row);
        case -VIEW_TYPE_ATTACHMENT:
            row = LayoutInflater.from(context).inflate(R.layout.item_incoming_attachment, parent, false);
            return new IncomingAttachmentViewHolder(row);
    }


    return null;
}

是否有一种方法可以实现使不同布局夸大的平滑滚动布局?

java android android-recyclerview android-viewholder
1个回答
0
投票

这里您需要一个视图支架和导入到其中的不同布局文件。

  • 请尝试将所有布局合并为一个。

  • 因此,基本上,当您需要一个视图时,可以将“可见性”设置为对其他视图无效,将“可见性”设置为特定视图。

© www.soinside.com 2019 - 2024. All rights reserved.