I have an app that contains a ViewPager
with its 3 Tabs. When going from the first to the second it does it normal, but from the second to the third it gets a little slow, it should be noted that in the third I have a gallery of images that are not so heavy with an 40.4K
approximate weight for the theme of the resolution, I don't know why it gets slow, but it's as if at the moment of scrolling the whole gallery is created again, Thanks.
Here is the code of tab2
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
View contentView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.fragment_dos, null);
return contentView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (RecyclerView) contentView.findViewById(R.id.reyclerViewDos);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new AdapterClase();
recyclerView.setAdapter(adapter);
}
}
this is your recycler
introducir el código aquí
public class AdapterClass extends RecyclerView.Adapter {
private static final int[] MIS_IMAGENES= new int[]{R.drawable.cold1, R.drawable.cold2};
private static final String[] MIS_CANCIONES = new String[]{"Mylo Xyloto", "A Head Full of Dreams"};
public class ViewHolderClase extends RecyclerView.ViewHolder {
TextView banda, cancion;
ImageView imageView;
public ViewHolderClase(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imagenes);
cancion = (TextView) itemView.findViewById(R.id.musica);
}
}
@Override
public ViewHolderClase onCreateViewHolder(ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, null);
return new ViewHolderClase(viewItem);
}
@Override
public void onBindViewHolder(ViewHolderClase holder, int position) {
holder.imageView.setBackgroundResource(MIS_IMAGENES[position]);
holder.cancion.setText(MIS_CANCIONES[position]);
}
@Override
public int getItemCount() {
return MIS_CANCIONES.length;
}
}
well this is from tab 3 precisely
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
View contentView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.fragment_tres, null);
return contentView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (RecyclerView) contentView.findViewById(R.id.reyclerViewTres);
layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
adapter = new AdapterAlbum();
recyclerView.setAdapter(adapter);
}
@Override
public void onClick(View v) {
}
} and this is your recycler
private static final int[] MIS_IMAGENES = new int[]{R.drawable.cold22, R.drawable.cover3, R.drawable.cover};
public class ViewHolderClase extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolderClase(View itemView) {
imageView = (ImageView) itemView.findViewById(R.id.cover);
}
}
@Override
public ViewHolderClase onCreateViewHolder(ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.album, null);
return new ViewHolderClase(viewItem);
}
@Override
public void onBindViewHolder(final ViewHolderClase holder, int position) {
holder.imageView.setBackgroundResource(MIS_IMAGENES[position]);
@Override
public int getItemCount() {
return MIS_IMAGENES.length;
}
}
The problem is probably caused by loading images on the main thread. First you have to make sure that
ImageView
the one you use in your Adapter doesn't have any default image in xml (android:src="@drawable/imagen_default"
). Second, use a library that loads images asynchronously. For ease of use I recommend using Piccaso .Following the instructions from its page, you can add it to your project.
Then in your method
onBindViewHolder
you replace it with the following:Greetings.