ListView在滚动时加载更多内容

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

我想让我的列表只从我的数组列表中加载20条消息,当我滚动到结束加载20多个,依此类推等等。

顺便说一句,列表是从底部填充的,最后的消息在底部显示为1,我希望它保持相同。加载较旧和较旧的邮件。

这是我的适配器现在的代码。

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

android listview lazy-loading infinite-scroll
1个回答
0
投票

首先,我假设您第一次创建适配器时,您只是传递了20条第一条消息。然后,您需要在需要时添加更多内容。这是有效的方法。并不是说您已经拥有列表中的所有消息,而只是在滚动到底部时显示它们。

考虑到这一点。

首先检测listView滚动到底部的时间。

检查这个问题如何做到这一点:

Find out if ListView is scrolled to the bottom?

在适配器中添加公共方法,并在检测到列表已滚动时调用它。

public void appendMessages(List<Messages> moreMessages){
  this.messages.addAll(moreMessages);
}

在那之后:

adapter.notifyDatasetChanged();
© www.soinside.com 2019 - 2024. All rights reserved.