Recyclerview with Show More button

Asked

Viewed 308 times

1

I have a recyclerview that pulls data from firebase, but it takes all the data at once, and this causes the application to lock, as I do for it to show only the first 10 and as the user goes down scrool it goes downloading more data?

Adapter

  public VideoListAdapter(@NonNull Activity context, @LayoutRes 
  int resource, @NonNull List<VideoUpload> objects) {
    super(context, R.layout.image_item, objects);
    this.context = context;
    listImage = objects;
}


@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    autenticacao = FirebaseAuth.getInstance();
    @SuppressLint("ViewHolder") View v = inflater.inflate(R.layout.image_item, parent, false);

    imgList = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Videos/");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            //Fetch image data from firebase database
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                VideoUpload video = snapshot.getValue(VideoUpload.class);
                string = video.getUrl();


            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    telefone = v.findViewById(R.id.contatoResponsavel);
    posicao = v.findViewById(R.id.posicaoJogador);
    idade = v.findViewById(R.id.idadeJogador);
    localidade = v.findViewById(R.id.localidadeJogador);
    TextView tvName = v.findViewById(R.id.tvImageName);
    imgYoutube = v.findViewById(R.id.videoPlayer);

    YouTubeThumbnailView youTubePlayerView = (YouTubeThumbnailView) v.findViewById(R.id.youtube_thumbnail);

    String tvNameString;
    final String linkFormatado;
    final String linkYoutube;
    linkYoutube = listImage.get(position).getUrl();

    linkFormatado = linkYoutube.replace("https://youtu.be/", "");
    posicao.setText("Posição: " + listImage.get(position).getPosicao());
    idade.setText("Idade: " + listImage.get(position).getIdade());
    localidade.setText("Localização: " + listImage.get(position).getEstado());
    tvNameString = listImage.get(position).getName();
    telefone.setText("Contato: " + listImage.get(position).getTelefone());

    tvName.setText("Nome: " + tvNameString);
    imgYoutube.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) getContext(),
                    "AIzaSyBvrGBKv7AU19B8DPp9ubiWPRS9FE7Ek9w",
                    linkFormatado,

                    100,
                    true,
                    false);
            getContext().startActivity(intent);


        }
    });


    return v;
}


}

Activity

    container = findViewById(R.id.container);

    anim = (AnimationDrawable) container.getBackground();
    anim.setEnterFadeDuration(2500);
    anim.setExitFadeDuration(5000);
    anim.start();


    imgList = new ArrayList<>();

   lv = findViewById(R.id.listViewImage);

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation2 = (BottomNavigationView) findViewById(R.id.navigation);
    navigation2.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Aguarde ....");
    progressDialog.show();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Videos/");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            progressDialog.dismiss();

            //Fetch image data from firebase database
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                //ImageUpload class require default constructor
                VideoUpload video = snapshot.getValue(VideoUpload.class);
                imgList.add(video);


            }


            adapter = new VideoListAdapter(HomeJogadorActivity.this, R.layout.image_item, imgList);
            //Set adapter for listview
            lv.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}

@Override
public void onBackPressed() {

}

}

1 answer

0

Try to use the setHasFixedSize() for the Recycler view this helps with the performance gain according to the documentation.

Example:

mRecyclerView.setHasFixedSize(true)

Java Doc:

Recyclerview can perform several optimizations if it can know in Advance that Recyclerview’s size is not affected by the Adapter Contents. Recyclerview can still change its size based on other factors (e.g. its Parent’s size) but this size calculation cannot Depend on the size of its Children or Contents of its Adapter (except the number of items in the Adapter).

If your use of Recyclerview Falls into this Category, set this to {@code true}. It will allow Recyclerview to avoid invalidating the Whole layout when its Adapter Contents change.

@param hasFixedSize true if Adapter changes cannot affect the size of the Recyclerview.

Then tell me if it worked, hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.