1

I would like to make my list only load 20 messages from my array list and when I scroll to the end load 20 more, and so on and so on.

BTW the list is populated from the bottom, the last messages are presented 1st at the bottom and I would like it to keep it the same. Loading older and older messages.

this is the code for my adapter now.

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;
        }


    }

its a little bit harde for me because of the different layouts.

What can I do to make it?

Thanks :D

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29

1 Answers1

0

First of all, I assume that the first time you create your adapter, you are just passing the 20 first messages. And then you will want to add more when needed. That is the efficient way of doing it. Not that you already have all the messages in your list and are just showing them when you scroll to the bottom.

With that in mind.

First detect when your listView is scrolled to the bottom.

Check this question how to do that:

Find out if ListView is scrolled to the bottom?

Add a public method in your Adapter and call it whenever you detect that your list was scrolled.

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

after that call:

adapter.notifyDatasetChanged();
psydj1
  • 181
  • 10