Tôi đang tải hình ảnh 400x200 trong RecyclerView, nhưng việc di chuyển bị lag trên các thiết bị 2k. Tôi đang sử dụng Picasso để tải hình ảnh từ tài nguyên.RecyclerView laggy scrolling
Như bạn có thể thấy trong hình ảnh demo bị mờ trên màn hình 2k, nhưng nếu tôi tải hình ảnh có độ phân giải cao thì tình hình trở nên tồi tệ hơn. Làm thế nào để sửa lỗi này, tôi thậm chí không tải hình ảnh lớn, 400x200 của nó?
Đây là mã của tôi Card_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
card_view:cardPreventCornerOverlap="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="2dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rel"
>
<ImageView
android:id="@+id/cardimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/p7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="24dp"
android:textStyle="bold"
android:textSize="24sp"
android:id="@+id/cardtitle"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Mã Myadapter
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private Context mContext;
List<Flower> list = new ArrayList<>();
public CardAdapter(Context mContext, List<Flower> list) {
this.mContext = mContext;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.Flower=getItem(position);
holder.cardtitle.setText(list.get(position).name);
Picasso.with(mContext)
.load(list.get(position).id)
.placeholder(R.drawable.ic_launcher)
.into(holder.cardimage);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return list.size();
}
public Flower getItem(int i) {
return list.get(i);
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView cardimage;
TextView cardtitle;
Flower Flower;
public ViewHolder(View itemView) {
super(itemView);
cardimage = (ImageView) itemView.findViewById(R.id.cardimage);
cardtitle = (TextView) itemView.findViewById(R.id.cardtitle);
}
}
}
UPDATE: Tôi tải hình ảnh từ tài nguyên, tôi không đang tải xuống không có gì
Đây là mảng tôi
private void initializeData() {
flowers = new ArrayList<>();
flowers.add(new Flower("Flower 1", R.drawable.p8));
flowers.add(new Flower("Flower 2", R.drawable.p10));
flowers.add(new Flower("Flower 3", R.drawable.p11));
flowers.add(new Flower("Flower 4", R.drawable.p8));
flowers.add(new Flower("Flower 5", R.drawable.photo2));
flowers.add(new Flower("Flower 6", R.drawable.photo6));
flowers.add(new Flower("Flower 7", R.drawable.p12));
flowers.add(new Flower("Flower 8", R.drawable.p9));
flowers.add(new Flower("Flower 9", R.drawable.p8));
flowers.add(new Flower("Flower 10", R.drawable.p8));
flowers.add(new Flower("Flower 11", R.drawable.p8));
flowers.add(new Flower("Flower 12", R.drawable.p10));
}
UPDATE 2: Guys Tôi cố định hầu hết các độ trễ bằng cách thiết lập adapter.setHasStableIds (true), nhưng ứng dụng vẫn còn lag trên cuộn giấy đầu tiên trong khi hình ảnh không được nạp chưa , cách khắc phục điều đó?
CẬP NHẬT 3: Tôi vừa thử tải hình ảnh từ web và mọi thứ có vẻ trơn tru, có thể có một số vấn đề khi tải hình ảnh từ tài nguyên.
Ok, cảm ơn các bạn, tôi sẽ tải hình ảnh của tôi từ web.
chỉ tụt hậu khi cuộn xuống ?? hoặc làm nó lag cho cả hai di chuyển lên và xuống? –
@LucasCrawford độ trễ của nó chỉ khi cuộn xuống. EDIT: của nó tụt hậu lên và xuống, một lần khi hình ảnh được tải có ít tụt hậu nhưng nó vẫn còn đó. –
bạn có tìm thấy giải pháp này không ?? –