[英]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."我的应用程序有问题,我使用了带有 cardViews 的 recyclerView,但是当我在我的设备中运行我的应用程序时,滚动非常缓慢,而且我在日志中也有这条消息:“I/Choreographer:跳过 42 帧!应用程序可能在其主线程上做了太多工作。” 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.
我是新来的,所以我研究了如何使用像 picasso 这样的库(我只使用 drawable 目录中的图像),但问题仍然存在,而且,我阅读了有关 assncTask 的信息,但我不确定我在哪里需要将 assyncTask 类放在我的代码中。 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: https : //github.com/bumptech/glide然后你的代码看起来像这样:
Glide.with(context).load(items.get(i).getImagen()).into(viewHolder.imagen);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.