IT修真院·坑乎 | Java(后端),CSS(前端),JavaScript(前端),PM(产品经理),UI(交互设计),QA(测试),Android(安卓APP),iOS(苹果APP),OP(运维),Python(后端)日常在线问答学习平台
咨询电话 : 010-59478634
切换导航
首页
我的提问
我的回答
我的点赞
消息通知
个人主页
×
提示
尚未登陆,前往官网登陆?
×
提示
尚未登陆,前往官网登陆?
CSS
JS
Android
IOS
JAVA
OP
PM
UI
QA
求职辅导
Python
运营
动画师
AIGC
CSS
JS
Android
IOS
JAVA
OP
PM
UI
QA
求职辅导
Python
运营
动画师
AIGC
所有任务
任务1
任务2
任务3
任务4
任务5
任务6
任务7
任务8
任务9
任务10
任务11
任务12
任务13
任务14
任务15
任务16
任务17
全部
问题
已收录
问题
未收录
问题
没有找到答案,去提问>>>
该问题已被官方收录
调用scrollToPositionWithOffSet不起作用
0
1
class="cursor-pointer"> 详细描述
更新RcyclerView适配器中的数据后,调用scrollToPositionWithOffSet(adapter.getItemCont() - 1, 0)方法让RecyclerView滚动到底部,不是每次都能生效
class="cursor-pointer"> 截图
class="cursor-pointer"> 代码
chatAdapter.addItem(msg); linearLayoutManager.scrollToPositionWithOffset(chatAdapter.getItemCount() - 1, 0); ############################################################################################ public class ChatAdapter extends BaseAdapter
{ private static final String TAG = "ChatAdapter"; //普通文本消息 private final int TYPE_TEXT_SEND = 0; private final int TYPE_TEXT_RECEIVER = 1; //图片 private final int TYPE_IMAGE_SEND = 2; private final int TYPE_IMAGE_RECEIVER = 3; //语音消息 private final int TYPE_VOICE_SEND = 4; private final int TYPE_VOICE_RECEIVER = 5; private String currentUid = ""; //构造 public ChatAdapter(Context context) { super(context); try { currentUid = BmobUser.getCurrentUser(User.class).getObjectId(); } catch (Exception e) { e.printStackTrace(); } } @Override @NonNull public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { switch (viewType) { case TYPE_TEXT_SEND: View sendTextView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_chat_right, parent, false); //设置item长按事件 sendTextView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (onItemLongClickListener != null) { onItemLongClickListener.onItemLongClick((Integer) view.getTag()); } return false; } }); //设置OnCreateContextMenuListener sendTextView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(1, 0, 1, "删除"); } }); return new SendTextViewHolder(sendTextView); case TYPE_TEXT_RECEIVER: View receiverTextView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_chat_left, parent, false); return new ReceiverTextViewHolder(receiverTextView); case TYPE_IMAGE_SEND: View sendImageView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_chat_image_right, parent, false); //设置item长按事件 sendImageView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (onItemLongClickListener != null) { onItemLongClickListener.onItemLongClick((Integer) view.getTag()); } return false; } }); //设置OnCreateContextMenuListener sendImageView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(1, 0, 1, "删除"); } }); return new SendImageViewHolder(sendImageView, onImageClickListener); case TYPE_IMAGE_RECEIVER: View receiverImageView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_chat_image_left, parent, false); return new ReceiverImageViewHolder(receiverImageView, onImageClickListener); default: return null; } } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { ((BaseMessageViewHolder) viewHolder).bindData(getItemList(), position); if (viewHolder instanceof SendTextViewHolder) { viewHolder.itemView.setTag(position); } else if (viewHolder instanceof ReceiverTextViewHolder) { // } else if (viewHolder instanceof SendImageViewHolder) { viewHolder.itemView.setTag(position); } else if (viewHolder instanceof ReceiverImageViewHolder) { viewHolder.itemView.setTag(position); } } @Override public int getItemViewType(int position) { BmobIMMessage msg = getItem(position); if (msg.getMsgType().equals(BmobIMMessageType.TEXT.getType())) { return msg.getFromId().equals(currentUid) ? TYPE_TEXT_SEND : TYPE_TEXT_RECEIVER; } else if (msg.getMsgType().equals(BmobIMMessageType.IMAGE.getType())) { return msg.getFromId().equals(currentUid) ? TYPE_IMAGE_SEND : TYPE_IMAGE_RECEIVER; } else if (msg.getMsgType().equals(BmobIMMessageType.VOICE.getType())) { return msg.getFromId().equals(currentUid) ? TYPE_VOICE_SEND : TYPE_VOICE_RECEIVER; } else { return -1; } } public OnImageClickListener onImageClickListener; //点击item事件接口 public interface OnImageClickListener { void onImageClick(int position); } //点击item事件监听 public void setOnImageClickListener(OnImageClickListener onImageClickListener) { this.onImageClickListener = onImageClickListener; } }
首页
1
末页
去第
页
确定
Copyright ©2015 北京葡萄藤信息技术有限公司 All Rights Reserved | 京ICP备15035574号-1
85
3
10