RecyclerView与图像滚动不平滑(滞后)

时间:2018-06-13 14:33:25

标签: java android performance android-recyclerview

我有一个常见问题 - recyclerView滞后并滚动不平滑。我已阅读了许多教程并实施了他们的方法,但没有成功。我在recycleView的每个raw中都有imageView和textView。我尝试使用像Glide或Picasso这样的图像加载器,但加载图像时有一些延迟。表现不好的原因是什么?这是行布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/text2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:paddingEnd="5dp"
    android:paddingStart="5dp"
    android:text="@string/textview"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="@string/image"
    android:scaleType="fitCenter"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text2" />

和列表布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"

&GT;

<android.support.v7.widget.RecyclerView
    android:id="@+id/list_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollingCache="false"
    android:animationCache="false"
    />

CustomAdapter:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private ArrayList<Image> DrinkArrayList;

public CustomAdapter(Context context, ArrayList<Image> DrinkArrayList) {
    this.context = context;
    this.DrinkArrayList = DrinkArrayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(
            LayoutInflater.from(context)
            .inflate(R.layout.my_image_list, parent, false));
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Image image = DrinkArrayList.get(position);
    holder.name1.setText(image.getName());
    holder.iv1.setImageResource(image.getImageResourceId());

}

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


public class ViewHolder extends RecyclerView.ViewHolder{
   private TextView name1;
   private ImageView iv1;

    public ViewHolder(View itemView) {
        super(itemView);
        this.name1 = itemView.findViewById(R.id.text2);
        this.iv1 = itemView.findViewById(R.id.imageView2);

    }
}

}

活动中的相关方法:

RecyclerView listDrinks2 = findViewById(R.id.list_image);
    listDrinks2.setAdapter(new CustomAdapter(this, DrinkArrayList));
    listDrinks2.setLayoutManager(new LinearLayoutManager(this));

0 个答案:

没有答案