微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – RecyclerView滞后滚动

我在RecyclerView中加载400×200的图像,但滚动在2k设备上是滞后的.我正在使用毕加索从资源加载图像.

正如您在演示中可以看到的图像在2k屏幕上模糊,但如果我加载更高分辨率的图像,情况变得更糟.
如何解决这个问题,我甚至没有加载大图像,它的400×200?

Demo

这是我的代码
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>

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

更新:我从资源加载图像,我没有下载什么

这是我的阵列

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

更新2:我们通过设置adapter.setHasstableIds(true)来修复大部分的滞后,但是应用程序在第一个滚动条件下仍然滞后,而图像尚未加载,如何解决

更新3:我刚刚尝试从Web加载图像,一切似乎顺利,可能是从资源加载图像有一些问题.

好的,谢谢你们,我将从网页加载我的图片.

解决方法

RecyclerView.setHasFixedSize()设置为true?我不能再现这个滞后…你可以把整个项目发贴到git吗?检查一下,也许它可以帮助你: http://antonioleiva.com/recyclerview/

原文地址:https://www.jb51.cc/android/313771.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐