升级适配器后,信息不会显示在底部
我不确切知道问题出在哪里 我需要指导,以便我可以找到有关此主题的信息 正如我在代码中看到的那样,我想在用户单击按钮时将添加的数据向下添加到底部。
mLayoutManager = new LinearLayoutManager(activity);
mLayoutManager.setStackFromEnd(true);
mLayoutManager.setReverseLayout(true);
adminchat.setLayoutManager(mLayoutManager);
adNewFullChat = new AdNewFullChat(activity , totalRestaurants);
adminchat.setAdapter(adNewFullChat);
mLayoutManager.smoothScrollToPosition(adminchat, null, totalRestaurants.size());
public void onClick(View view) {
String text = chattext.getText().toString();
if (text.length() > 0) {
final Chat chat = new Chat();
chat.setTag("admin");
chat.setTimestamp(format);
chat.setUserid(userid);
totalRestaurants.add(chat);
adNewFullChat.notifyDataSetChanged();
if (adNewFullChat.getItemCount() > 1) {
adminchat.getLayoutManager().smoothScrollToPosition(adminchat, null, adNewFullChat.getItemCount() - 1);
}
适配器代码: 我不确切知道工作在哪里
public AdNewFullChat(Context context, List<Chat> messageList) {
mContext = context;
mMessageList = messageList;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
@Override
public int getItemCount() {
return mMessageList.size();
}
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
Chat message = (Chat) mMessageList.get(position);
if (message.getTag().equals("user")) {
return VIEW_TYPE_MESSAGE_RECEIVED;
} else if (message.getTag().equals("admin")) {
return VIEW_TYPE_MESSAGE_SENT;
}
return 0;
}
// Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_message, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.their_message, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Chat message = (Chat) mMessageList.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
答案 0 :(得分:0)
感谢您添加代码。
您需要更新适配器上的mMessageList
列表。
更新totalRestaurants
不会修改适配器中的数据,您可以做的就是在适配器中使用如下方法:
public void addElement(Chat chat) { mMessageList.add(chat) }
您这样称呼它:
adNewFullChat.addElement(chat);
代替totalRestaurants.add(chat);
编辑:哦,我没有看到您正在使用mLayoutManager.setReverseLayout(true);
如果删除该行,新元素将添加到底部