我在片段中制作了一个recyclerView。它在每个项目中都有一个ImageView,并且该图像是通过URL从Picasso加载的。但是问题是,当我第一次滚动时,recyclerView会滞后,但是当数据加载时,它会正常滚动。我试图删除onBindViewHolder中的所有操作,但问题仍然存在。然后,我删除了项目布局的复杂部分,但仍然没有任何改进。我在Snapdragon 845上运行,因此手机的性能没有问题,我在其他设备上尝试了同样的操作,但效果不佳。我什至尝试了硬件加速,但没有帮助。
这是我的适配器:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieHolder> {
private List<Movie> mMovies;
private Context mContext;
public MovieAdapter(Context mContext, List<Movie> mMovies) {
this.mContext = mContext;
this.mMovies = mMovies;
}
@NonNull
@Override
public MovieHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_movie_movie_layout, viewGroup, false);
return new MovieHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MovieHolder movieHolder, int i) {
Movie movie = mMovies.get(i);
if (movie.getTitle2().equals("")) {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.GONE);
} else {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.VISIBLE);
movieHolder.mTextViewTitle2.setText(movie.getTitle2());
}
movieHolder.mTextViewGenre.setText(movie.getGenre());
movieHolder.mRatingBar.setRating((float) 4.5);
String url = "https://image.tmdb.org/t/p/w342";
url += movie.getPoster_path();
Transformation transformation = new Rounded(8, ALL);
Picasso.get().load(url)
.placeholder(R.color.colorAccent)
.transform(transformation)
.resize(100, 150)
.centerCrop()
.into(movieHolder.mImageViewPoster);
}
@Override
public int getItemCount() {
return mMovies.size();
}
public class MovieHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageViewPoster;
private TextView mTextViewTitle1;
private TextView mTextViewTitle2;
private TextView mTextViewGenre;
private RatingBar mRatingBar;
public MovieHolder(@NonNull View itemView) {
super(itemView);
mImageViewPoster = itemView.findViewById(R.id.image_view_poster);
mTextViewTitle1 = itemView.findViewById(R.id.text_view_title_1);
mTextViewTitle2 = itemView.findViewById(R.id.text_view_title_2);
mTextViewGenre = itemView.findViewById(R.id.text_view_genre);
mRatingBar = itemView.findViewById(R.id.rating_bar);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Movie movie = mMovies.get(getAdapterPosition());
Intent intent = new Intent(mContext, MovieActivity.class);
intent.putExtra("EXTRA", movie);
mContext.startActivity(intent);
}
}
}
这是我的物品布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="192dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.CardView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="8dp"
android:elevation="0dp"
app:cardBackgroundColor="@color/colorAccent"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</android.support.v7.widget.CardView>
<ImageView
android:id="@+id/image_view_poster"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginStart="12dp"
android:layout_marginBottom="12dp"
android:adjustViewBounds="true"
android:elevation="16dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:elevation="8dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/image_view_poster"
app:layout_constraintTop_toTopOf="@+id/imageView">
<TextView
android:id="@+id/text_view_title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="16dp" />
<TextView
android:id="@+id/text_view_title_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.3"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="12dp" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<android.support.v7.widget.AppCompatRatingBar
android:id="@+id/rating_bar"
style="@style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:outlineProvider="paddedBounds"
android:progressBackgroundTint="#FFFFFF"
android:progressTint="#FECE00"
android:stepSize="0.5" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<TextView
android:id="@+id/text_view_genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="Action | Adventure"
android:textSize="12dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>