我想使列表仅从阵列列表中加载20条消息,并且当我滚动到末尾时再加载20条消息,依此类推。
顺便说一句,列表从底部开始填充,最后一条消息在底部显示1st,我希望它保持不变。加载越来越多的消息。
这是我的适配器的代码。
public class MensagemAdapter extends ArrayAdapter<Message> {
private Context context;
private List<Message> messages;
private List<Users> users;
public MensagemAdapter(@NonNull Context context, @NonNull List<Message> objects, @NonNull List<Users> users) {
super(context, 0, objects);
this.context=context;
this.messages = objects;
this.users = users;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = null;
if (messages != null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
Message message = messages.get(position);
Log.d("Num", "getView: " +message.getUserId() + " " + message.getId());
if(message.getUserId()==1 ){
assert inflater != null;
view = inflater.inflate(R.layout.right_no_attach,parent,false);
TextView textoMensagem = view.findViewById(R.id.tv_mensage);
textoMensagem.setText(message.getContent());
if (message.getAttachments() !=null){
for(int i=0; i<message.getAttachments().size();i++){
//view = inflater.inflate(R.layout.right_attach,parent,false);
if (i==0){
ImageView attach = view.findViewById(R.id.imageView1);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else if (i==1){
ImageView attach = view.findViewById(R.id.imageView2);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else if(i==2){
ImageView attach = view.findViewById(R.id.imageView3);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else {
ImageView attach = view.findViewById(R.id.imageView3);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
}
}
}
}else{
assert inflater != null;
view = inflater.inflate(R.layout.left_no_attach,parent,false);
TextView nomeMensagem = view.findViewById(R.id.nomeId);
nomeMensagem.setText(users.get(message.getUserId()-1).getName());
ImageView avatar = view.findViewById(R.id.avatarIm);
Picasso.get().load(users.get(message.getUserId()-1).getAvatarId()).transform(new CircleTransform()).into(avatar);
TextView textoMensagem = view.findViewById(R.id.tv_mensage);
textoMensagem.setText(message.getContent());
if (message.getAttachments() !=null){
for(int i=0; i<message.getAttachments().size();i++){
//view = inflater.inflate(R.layout.right_attach,parent,false);
if (i==0){
ImageView attach = view.findViewById(R.id.imageViewA);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else if (i==1){
ImageView attach = view.findViewById(R.id.imageViewB);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else if(i==2){
ImageView attach = view.findViewById(R.id.imageViewC);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
} else {
ImageView attach = view.findViewById(R.id.imageViewD);
attach.setVisibility(View.VISIBLE);
Picasso.get().load( message.getAttachments().get(i).getThumbnailUrl()).into(attach);
}
}
}
}
if (message.getAttachments() ==null) {
TextView textoMensagem = view.findViewById(R.id.tv_mensage);
textoMensagem.setText(message.getContent());
}
}
return view;
}
}
由于布局不同,这对我来说有点困难。
我该怎么做?
谢谢:D
答案 0 :(得分:0)
首先,我假设第一次创建适配器时,您只是传递了20条第一条消息。然后,您将需要在需要时添加更多内容。 这是有效的方法。 不是,您已经将所有消息都包含在列表中,并在滚动到底部时仅显示它们。
请牢记这一点。
首先检测listView何时滚动到底部。
检查此问题的方法:
Find out if ListView is scrolled to the bottom?
在适配器中添加公共方法 并在您发现列表已滚动时调用它。
public void appendMessages(List<Messages> moreMessages){
this.messages.addAll(moreMessages);
}
在通话之后:
adapter.notifyDatasetChanged();