Scrolling a ViewPager

3

I have an app that contains a ViewPager with its 3 Tabs. When going from the first to the second it is normal, but from the second to the third it gets a bit slow, it should be noted that in the third one I have a gallery of images that are not so heavy with a weight of 40.4K approximately the issue of the resolution, I do not know why it gets slow, but it's as if at the time of moving the whole gallery is created again, Thank you.

Here is the code for 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 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;
}

}

    
asked by Gunnar 29.03.2016 в 02:14
source

1 answer

2

Probably the problem happens because of loading images in the main thread. First you must make sure that in ImageView that you use in your Adapter does not have any default image in xml ( android:src="@drawable/imagen_default" ). Second, it uses a library that performs image loading asynchronously. For ease of use I recommend using Piccaso .

Following the instructions from your page, you can add it to your project.

Then in your method onBindViewHolder you replace it with the following:

@Override
public void onBindViewHolder(final ViewHolderClase holder, int position) {
    Picasso.with(holder.imageView.getContext()).load(MIS_IMAGENES[position]).into(holder.imageView);    
}

Greetings.

    
answered by 04.04.2016 / 17:01
source