RecyclerView:错误的滚动效果 (Android)
RecyclerView: buggy scroll effect (Android)
这是我在 Whosebug 上的第一个 post。我对水平 androidx.recyclerview.widget.RecyclerView
有疑问:滚动效果确实有问题。我正在使用自定义适配器,但我认为这不是问题所在。我也在使用毕加索的图像。
- RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/test2"
android:paddingTop="10dp"
android:nestedScrollingEnabled="false"
android:clipToPadding="false"
android:clipChildren="false"
android:overScrollMode="never"
tools:ignore="MissingConstraints"/>
- MainFilmsListAdapter.java(我的适配器):
public class MainFilmsListAdapter extends RecyclerView.Adapter<MainFilmsListAdapter.MyViewHolder> {
private List<MainFilms> filmsList;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mainFilmsTitle, mainFilmsURL;
ImageView mainFilmsImage;
MyViewHolder(View view) {
super(view);
mainFilmsTitle = view.findViewById(R.id.main_films_item_title);
mainFilmsURL = view.findViewById(R.id.main_films_item_url);
mainFilmsImage = view.findViewById(R.id.main_films_item_image);
}
}
public MainFilmsListAdapter(List<MainFilms> filmsList) {
this.filmsList = filmsList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.main_films_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MainFilms film = filmsList.get(position);
holder.mainFilmsTitle.setText(film.getMainFilmsTitle());
holder.mainFilmsURL.setText(film.getMainFilmsURL());
Picasso.get()
.load(filmsList.get(position).getMainFilmsImage())
.fit()
.centerCrop()
.into(holder.mainFilmsImage);
}
@Override
public int getItemCount() {
return filmsList.size();
}
}
- MainFilms.java:
public class MainFilms {
String mainFilmsTitle;
String mainFilmsImage;
String mainFilmsURL;
public MainFilms(String mainFilmsTitle, String mainFilmsURL, String mainFilmsImage) {
this.mainFilmsTitle = mainFilmsTitle;
this.mainFilmsURL = mainFilmsURL;
this.mainFilmsImage = mainFilmsImage;
}
public String getMainFilmsTitle() {
return mainFilmsTitle;
}
public String getMainFilmsURL() {
return mainFilmsURL;
}
public String getMainFilmsImage() {
return mainFilmsImage;
}
}
- MainActivity.java(回收视图):
mAdapter = new MainFilmsListAdapter(filmsList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
- 结果: https://i.imgur.com/64CPQDY.mp4(因为文件太大无法上传,质量不好请见谅,但我的电脑很慢)
想不通问题出在哪里,找了5个多小时的解决方法。我只注意到视图在到达 RecyclerView 中的某个项目时冻结。我还在 Android 手机上测试了该应用程序。 RecyclerView中的itmes数量是恒定的。
图像加载似乎有问题。
您可以尝试阅读此相关问答 - Recyclerview painfully slow to load cached images form Picasso
这是我在 Whosebug 上的第一个 post。我对水平 androidx.recyclerview.widget.RecyclerView
有疑问:滚动效果确实有问题。我正在使用自定义适配器,但我认为这不是问题所在。我也在使用毕加索的图像。
- RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/test2"
android:paddingTop="10dp"
android:nestedScrollingEnabled="false"
android:clipToPadding="false"
android:clipChildren="false"
android:overScrollMode="never"
tools:ignore="MissingConstraints"/>
- MainFilmsListAdapter.java(我的适配器):
public class MainFilmsListAdapter extends RecyclerView.Adapter<MainFilmsListAdapter.MyViewHolder> {
private List<MainFilms> filmsList;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mainFilmsTitle, mainFilmsURL;
ImageView mainFilmsImage;
MyViewHolder(View view) {
super(view);
mainFilmsTitle = view.findViewById(R.id.main_films_item_title);
mainFilmsURL = view.findViewById(R.id.main_films_item_url);
mainFilmsImage = view.findViewById(R.id.main_films_item_image);
}
}
public MainFilmsListAdapter(List<MainFilms> filmsList) {
this.filmsList = filmsList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.main_films_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MainFilms film = filmsList.get(position);
holder.mainFilmsTitle.setText(film.getMainFilmsTitle());
holder.mainFilmsURL.setText(film.getMainFilmsURL());
Picasso.get()
.load(filmsList.get(position).getMainFilmsImage())
.fit()
.centerCrop()
.into(holder.mainFilmsImage);
}
@Override
public int getItemCount() {
return filmsList.size();
}
}
- MainFilms.java:
public class MainFilms {
String mainFilmsTitle;
String mainFilmsImage;
String mainFilmsURL;
public MainFilms(String mainFilmsTitle, String mainFilmsURL, String mainFilmsImage) {
this.mainFilmsTitle = mainFilmsTitle;
this.mainFilmsURL = mainFilmsURL;
this.mainFilmsImage = mainFilmsImage;
}
public String getMainFilmsTitle() {
return mainFilmsTitle;
}
public String getMainFilmsURL() {
return mainFilmsURL;
}
public String getMainFilmsImage() {
return mainFilmsImage;
}
}
- MainActivity.java(回收视图):
mAdapter = new MainFilmsListAdapter(filmsList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
- 结果: https://i.imgur.com/64CPQDY.mp4(因为文件太大无法上传,质量不好请见谅,但我的电脑很慢)
想不通问题出在哪里,找了5个多小时的解决方法。我只注意到视图在到达 RecyclerView 中的某个项目时冻结。我还在 Android 手机上测试了该应用程序。 RecyclerView中的itmes数量是恒定的。
图像加载似乎有问题。
您可以尝试阅读此相关问答 - Recyclerview painfully slow to load cached images form Picasso