Colleagues, new day and new question. It slows down the scrolling of the list, as if it is a very hard game, and I have a weak video card and there is no RAM in the computer at all. Moreover, it slows down both at the first scrolling down (when creating view holders) and when returning to already existing elements. The number of elements in the list is limited, each element consists of 3 ImageView (48x48, 16x16, 48x48) and 3 TextView. Two of the three vector images. Data loads from the Internet and caches to the list (ArrayList), a bitmap image (cover) is loaded via Glide.
Adapter code:
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; } } List Item XML Code:
<?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> The second (night) differs only in colors.
RecyclerView call code:
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); What is most interesting is that it does not slow down on all phones and it does not depend on the parameters of the phone (on my own, more relaxed, flies, on the phone of a colleague, more powerful, lags terribly, it’s already disgusting to look).