Коллеги, новый день и новый вопрос. Тормозит прокрутка списка, так, будто это очень тяжёлая игра, а у меня слабая видеокарта да и оперативки в компьютере совсем нет. Причём, тормозит и при первом прокручивании вниз (при создании вьюХолдеров) и при возврате к уже существующим элементам. Количество элементов в списке ограничено, состоит каждый элемент из 3х ImageView (48х48, 16х16, 48х48) и 3х TextView. Два из трёх изображений векторные. Данные грузит с интернета и кэширует в список (ArrayList), растровое изображение (обложка) грузится через Glide.
Код адаптера:
private static ArrayList <Book> books;
static Context context;
final static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView author;
TextView length;
ImageView icon;
ImageView bookType;
ImageView bookFace;
ViewHolder (View view) {
super (view);
context = view.getContext();
this.name = view.findViewById (R.id.name);
this.author = view.findViewById (R.id.author);
this.length = view.findViewById (R.id.timeOrPages);
this.icon = view.findViewById (R.id.icon);
this.bookType = view.findViewById (R.id.bookType);
this.bookFace = view.findViewById (R.id.bookFace);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListActivity listActivity = new ListActivity ();
int position = getLayoutPosition();
if (MainActivity.onLine) {
if (books.get(position) instanceof AudioBook) {
BookActivity.bookType = "audio";
BookActivity.book = books.get (position);
if (books.get (position).getChapters() != null && books.get (position).getChapters().size() == 1) {
BookActivity.actualPosition = (int) books.get (position).getChapters().get(0).currentPosition;
}
BookActivity.actualChapter = getActualChapter (position);
BookActivity.dataHttp = books.get (position).getSystemName();
context.startActivity(new Intent(context, BookActivity.class));
listActivity.books = null;
} else {
BookActivity.bookType = "text";
BookActivity.book = books.get (position);
BookActivity.actualChapter = getActualChapter(position);
BookActivity.dataHttp = books.get (position).getSystemName();
listActivity.books = null;
context.startActivity(new Intent(context, BookActivity.class));
}
} else {
Log.d (LOG_TAG, "chapter 0 = " + books.get (position).getChapters());
String [] path = books.get (position).getChapters().get (0).getAddress().split ("/");
String [] bookData = path [path.length - 2].split ("_");
switch (path [path.length - 3]) {
case ("audio"):
BookActivity.bookType = "audio";
BookActivity.title = bookData [1];
BookActivity.author = bookData [2];
BookActivity.actualChapter = getActualChapter(position);
BookActivity.book = getBook (bookData [1], "audio");
context.startActivity(new Intent(context, BookActivity.class));
listActivity.books = null;
break;
case ("text"):
BookActivity.bookType = "text";
BookActivity.title = bookData [1];
BookActivity.author = bookData [2];
BookActivity.actualChapter = getActualChapter(position);
BookActivity.book = getBook (bookData [1], "text");
context.startActivity(new Intent(context, BookActivity.class));
listActivity.books = null;
break;
}
}
}
});
}
}
public SomeAdapter (ArrayList <Book> books) {
this.books = books;
}
@Override
public final SomeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d (LOG_TAG, "onCreate viewHolder");
View v;
if (MainActivity.night)
v = LayoutInflater.from (parent.getContext()).inflate(R.layout.list_item_night, parent, false);
else
v = LayoutInflater.from (parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder (v);
}
@Override
public final void onBindViewHolder(final NewBiblioAdapter.ViewHolder holder, final int position) {
holder.name.setText (books.get (position).getName());
holder.author.setText (books.get (position).getAuthor());
holder.length.setText(books.get(position).getLength());
if (books.get (position) instanceof AudioBook) {
holder.icon.setBackgroundResource(R.drawable.iconaudio);
holder.bookType.setBackgroundResource(R.drawable.iconaudio);
}
else {
holder.icon.setBackgroundResource (R.drawable.icontext);
holder.bookType.setBackgroundResource (R.drawable.icontext);
}
try {
Glide.with (context)
.load (books.get (position).getCoverPreview ())
.into (holder.bookFace);
} catch (Exception e) {
Log.d (LOG_TAG, "thread exception", e);
}
}
@Override
public final void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public final int getItemCount() {
if (books == null)
return 0;
else
return books.size();
}
private static Book getBook (String bookName, String type) {
for (int i = 0; i < books.size(); i++) {
if (books.get (i).getName().equalsIgnoreCase (bookName)) {
return books.get(i);
}
}
if (type.equalsIgnoreCase ("audio"))
return new AudioBook ();
else
return new TextBook ();
}
private static int getActualChapter (int position) {
if (books.get (position).getChapters() != null && books.get (position).getChapters().size() == 1) {
return (int) books.get(position).getChapters().get(0).getId();
}
else
return 0;
}
}
Код XML элемента списка:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/SomeTheme"
android:background="@color/colorBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/bookFace"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:layout_margin="8dp" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textSize="10sp"
app:layout_constraintTop_toBottomOf="@id/name" />
<ImageView
android:id="@+id/icon"
android:layout_width="18dp"
android:layout_height="15dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/author" />
<TextView
android:id="@+id/timeOrPages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toRightOf="@id/icon"
app:layout_constraintTop_toBottomOf="@id/author" />
</android.support.constraint.ConstraintLayout>
<ImageView
android:id="@+id/bookType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="8dp" />
</LinearLayout>
Второй (night) отличается только цветами.
Код вызова RecyclerView:
recyclerView = findViewById (R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager (ListActivity.this);
recyclerView.setLayoutManager (layoutManager);
recyclerView.setHasFixedSize (true);
recyclerView.setNestedScrollingEnabled (false);
DividerItemDecoration verticalDividerDecorator = new DividerItemDecoration (ListActivity.this, DividerItemDecoration.VERTICAL);
Drawable horizontalDivider;
if (MainActivity.night)
horizontalDivider = ContextCompat.getDrawable (ListActivity.this, R.drawable.divider_night);
else
horizontalDivider = ContextCompat.getDrawable (ListActivity.this, R.drawable.divider);
try {
verticalDividerDecorator.setDrawable(horizontalDivider);
recyclerView.addItemDecoration (verticalDividerDecorator);
} catch (Exception e) {
Log.d (LOG_TAG, "divider exception", e);
}
adapter = new SomeAdapter((ArrayList<Book>) books);
recyclerView.setAdapter(adapter);
Что самое интересное - тормозит не на всех телефонах и зависит это не от параметров телефона (на моём, послабже, летает, на телефоне коллеги, помощнее, лагает ужасно, аж смотреть противно).