简体   繁体   中英

aplication with recyclerView and cardView is laggy when scrolling

I have a issues with my aplication, i used a recyclerView with cardViews, but when i run my aplication in my device, the scrolling is so laggy, also i have this message in the log: "I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread." I'm new in this, so i investigate how to use a library like picasso (I'm using only images from the drawable directory), but the problem persist, also, i read about assincTask, but i'm not sure where i need to put the assyncTask class in my code. I will apreciate all the help that can provide me.

MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView recycler;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager lManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<InfoCards> items = new ArrayList<>();
        items.add(new InfoCards(R.drawable.arduino_uno_2, getString(R.string.name_card1), getString(R.string.info_card1)));
        items.add(new InfoCards(R.drawable.hc05_bluetooth_module, getString(R.string.name_card2), getString(R.string.info_card2)));
        items.add(new InfoCards(R.drawable.ciruit_detail, getString(R.string.name_card3), getString(R.string.info_card3)));
        items.add(new InfoCards(R.drawable.ciruit_detail, getString(R.string.name_card4), getString(R.string.info_card4)));

        setContentView(R.layout.activity_main);


        recycler = (RecyclerView) findViewById(R.id.recycler_view);
        recycler.setHasFixedSize(true);


        lManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recycler.setLayoutManager(lManager);


        adapter = new CardAdapter(this, items);
        adapter.setHasStableIds(true);
        recycler.setAdapter(adapter);


    }

}

CardAdapter

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {

    private List<InfoCards> items;
    private Context context;


    public CardAdapter(Context context, List<InfoCards> items) {
        this.context = context;
        this.items = items;
    }

    public static class CardViewHolder extends RecyclerView.ViewHolder {

        public ImageView imagen;
        public TextView nameCard;
        public TextView information;
        public View view;

        public CardViewHolder(View v) {
            super(v);

            imagen = (ImageView) v.findViewById(R.id.imagen_cardview);
            nameCard = (TextView) v.findViewById(R.id.name_cardview);
            information = (TextView) v.findViewById(R.id.info_cardview);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final Intent intent;
                    switch (getLayoutPosition()) {
                        case 0:
                            intent = new Intent(v.getContext(), ArduinoCard.class);
                            v.getContext().startActivity(intent);
                            break;

                        case 1:
                            intent = new Intent(v.getContext(), BluetothCard.class);
                            v.getContext().startActivity(intent);
                            break;

                        case 2:
                            intent = new Intent(v.getContext(), HowBuildCard.class);
                            v.getContext().startActivity(intent);
                            break;

                        case 3:
                            intent = new Intent(v.getContext(), AplicationCard.class);
                            v.getContext().startActivity(intent);
                            break;
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
        return new CardViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CardViewHolder viewHolder, final int i) {

        //Bitmap bitmap = BitmapFactory.decodeResource(res, items.get(i).getImagen());
        //viewHolder.imagen.setImageBitmap(bitmap);

        //Picasso.with(context).load(items.get(i).getImagen()).into(viewHolder.imagen);
        viewHolder.imagen.setImageResource(items.get(i).getImagen());
        viewHolder.nameCard.setText(items.get(i).getNameCard());
        viewHolder.information.setText(String.valueOf(items.get(i).getInformation()));
    }
}

InfoCards

public class InfoCards {
    private int imagen;
    private String nameCard;
    private String information;

    public InfoCards(int imagen, String nameCard, String information) {
        this.imagen = imagen;
        this.nameCard = nameCard;
        this.information = information;
    }

    public String getNameCard() {
        return nameCard;
    }

    public String getInformation() {
        return information;
    }

    public int getImagen() {
        return imagen;
    }
}

your issue is most definitely

viewHolder.imagen.setImageResource(items.get(i).getImagen());

I suggest you use Glide: https://github.com/bumptech/glide then your code will look smth like this:

Glide.with(context).load(items.get(i).getImagen()).into(viewHolder.imagen);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM