在滚动时,使用recyclerView和cardView的应用程序是滞后的

时间:2016-03-21 00:57:04

标签: android android-recyclerview android-cardview

我的aplication存在问题,我使用了带有cardViews的recyclerView,但是当我在我的设备中运行我的应用程序时,滚动是如此滞后,我在日志中也有这条消息:“I / Choreographer:Skipped 42框架!应用程序可能在其主线程上做了太多工作。“ 我是新手,所以我调查如何使用像毕加索这样的库(我只使用可绘制目录中的图像),但问题仍然存在,我也读到了关于assincTask,但我不知道我在哪里需要将assyncTask类放在我的代码中。我将提供可以为我提供的所有帮助。

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

的InfoCard

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

1 个答案:

答案 0 :(得分:3)

你的问题绝对是

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

我建议您使用Glide:https://github.com/bumptech/glide 然后你的代码看起来像这样:

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