我正在创建一个聊天应用程序,根据收到的消息类型,该应用程序将具有不同的布局类型。每个视图持有者都有自己的布局和实现。该应用程序工作正常,但在滚动时会滞后,尤其是在布局类型更改时。
@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;
}
有没有办法实现一个平滑的滚动布局
你需要的是一个视图支架和导入到其中的不同布局文件。
尝试将所有布局合并为一个。
因此,基本上,你要做的就是在需要一个视图时可以将“可见性”设置为对其他视图无效,将“可见性”设置为特定视图。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。