About 700 items are transferred to RecyclerView, which takes about 2-3 seconds to display before it is displayed.
How to solve this problem by making the display of 50 elements at least (and manually “pull-up” load another + 50 elements)? Thank you in advance!
And you can poke me also on similar questions, or the necessary libraries for this?
What code is required of me? adapter?
adapter code:
public class AllStationActivityAdapter extends RecyclerView.Adapter<AllStationActivityAdapter.ViewHolder> { private static int MODE_DIRECTORY = 0; private static int MODE_FAVORITES = 1; public boolean colorTitleStation = true; public interface Listener { void onStationClicked(final Station station); } private final List<Station> stations = new LinkedList<>(); private Listener listener; private int mode; private static String descriptionInfo; private static String descriptionInfoFinal; private static String nameInfo; private static String nameInfoFinal; static MaterialStyledDialog dialog5; public AllStationActivityAdapter() { stations.addAll(getStations()); } @Override public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); final View view = inflater.inflate(R.layout.directory_item_two, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final Station station = stations.get(position); holder.nameView.setText(station.getName()); holder.genre.setText(String.format("%s", station.getNetwork())); holder.online.setText(String.format(""+"%s", position+1)); //holder.descriptionView.setText(String.format("%s", station.getDescription())); Picasso.with(holder.itemView.getContext()).load(station.getIconUrl()).transform(new Transformation() { @Override public Bitmap transform(final Bitmap source) { PaletteCache.generate(station.getIconUrl(), source); return source; } @Override public String key() { return station.getIconUrl(); } }).into(holder.iconView, new Callback() { @Override public void onSuccess() { final Palette palette = PaletteCache.get(station.getIconUrl()); if (palette == null) return; final Palette.Swatch swatch = palette.getVibrantSwatch(); if (swatch != null) { } // ставим цвет заголовка названия if (!colorTitleStation){ if (swatch != null) { holder.nameView.setTextColor(swatch.getRgb()); colorTitleStation = false; } } else { colorTitleStation = true; } } @Override public void onError() { } }); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { if (listener != null) listener.onStationClicked(station); // holder.itemView.startAnimation(new CustomAnimation().AnimationItem(AllStationActivityAdapter.this)); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { descriptionInfo = String.valueOf(holder.getLayoutPosition()); descriptionInfoFinal = stations.get(Integer.parseInt(descriptionInfo)).getDescription(); nameInfo = String.valueOf(holder.getLayoutPosition()); nameInfoFinal = stations.get(Integer.parseInt(nameInfo)).getName(); String img; img = String.valueOf(holder.getLayoutPosition()); dialog5 = new MaterialStyledDialog.Builder(PlayerView.favoriteAdd.getContext()) .setTitle(nameInfoFinal) // Название .setHeaderDrawable(R.drawable.playerview) .setIcon(holder.iconView.getDrawable()) .setDescription(descriptionInfoFinal) // Описание .withDialogAnimation(false, Duration.FAST) .withIconAnimation(true) .setPositiveText("OK") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog5.cancel(); } }).show(); return true; } }); } @Override public long getItemId(final int position) { return position; } @Override public int getItemCount() { return stations.size(); } private List<Station> getStations() { if (mode == MODE_FAVORITES) { return Favorites.getFavorites(); } return ArrayListMain.getStations(); } public void showFavorites() { mode = MODE_FAVORITES; updateStations(); } public void showDirectory() { mode = MODE_DIRECTORY; updateStations(); } public void setListener(final Listener listener) { this.listener = listener; } public void filterStations(final String query) { List<Station> filteredStations = queryStations(query); updateStations(filteredStations); } public void updateStations(final List<Station> filteredStations) { removeFilteredStations(filteredStations); addFilteredStations(filteredStations); } public void updateStations() { updateStations(getStations()); } private void removeFilteredStations(final List<Station> filteredStations) { final Iterator<Station> iterator = stations.iterator(); while (iterator.hasNext()) { final Station station = iterator.next(); if (!filteredStations.contains(station)) { final int position = stations.indexOf(station); iterator.remove(); notifyItemRemoved(position); } } } private void addFilteredStations(final List<Station> filteredStations) { for (Station station : filteredStations) { if (!stations.contains(station)) { final int position = findPosition(station); stations.add(position, station); notifyItemInserted(position); } } } private int findPosition(final Station station) { int position = 0; while(position < stations.size() && stations.get(position).getName().compareToIgnoreCase(station.getName()) < 0) { position++; } return position; } private List<Station> queryStations(final String query) { final List<Station> filtered = new java.util.ArrayList(); final boolean isEmpty = TextUtils.isEmpty(query); for (Station station : getStations()) { if (isEmpty || station.matchesQuery(query)) filtered.add(station); } return filtered; } public static class ViewHolder extends RecyclerView.ViewHolder { ImageView iconView; TextView nameView, genre, online; //TextView descriptionView; public ViewHolder(final View itemView) { super(itemView); iconView = (ImageView)itemView.findViewById(R.id.icon); nameView = (TextView)itemView.findViewById(R.id.name); //descriptionView = (TextView)itemView.findViewById(R.id.description); genre = (TextView)itemView.findViewById(R.id.genre); online = (TextView)itemView.findViewById(R.id.onlineView); } }} Activation code in which RecyclerView is displayed:
public class Genre_NewYear extends AppCompatActivity implements View.OnClickListener, Adapter_NewYear.Listener, SearchView.OnQueryTextListener, PlayerView.Listener { ... private RecyclerView directory; private PlayerView playerView; public Adapter_NewYear adapter; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_genre); directory = findViewById(R.id.directory); directory.setHasFixedSize(true); directory.setLayoutManager(new GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false)); directory.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isAcceptingText()) { imm.hideSoftInputFromWindow(directory.getWindowToken(), 0); } } } }); } adapter = new Adapter_NewYear(); directory.setAdapter(adapter); adapter.setListener(this); ... } ... private void showDirectory() { adapter.showDirectory(); } ... } Favorites collection code:
public class Favorites { public static ArrayList<String> favoriteUrls = null; private static File getFavoritesPath() { return new File(CacheManager.getRoot(), "favorites"); } public static void load() { try { Sack.open(String[].class, getFavoritesPath()).load(new Sack.Listener<String[]>() { @Override public void onResult(final Sack.Status status, final String[] favorites) { favoriteUrls = new ArrayList<>(); if (favorites == null) return; favoriteUrls.addAll(Arrays.asList(favorites)); } }); } catch (Exception e){ e.printStackTrace(); } } public static List<Station> getFavorites() { final List<Station> favorites = new ArrayList<>(); for (String url : favoriteUrls) favorites.add(ArrayListMain.getStation(url)); Station.sort(favorites); return favorites; } public static void addFavorite(final String favoriteUrl) { if (!favoriteUrls.contains(favoriteUrl)) { favoriteUrls.add(favoriteUrl); commit(); } } public static void removeFavorite(final String favoriteUrl) { if (favoriteUrls.contains(favoriteUrl)) { favoriteUrls.remove(favoriteUrl); commit(); } } private static void commit() { final String[] urls = favoriteUrls.toArray(new String[favoriteUrls.size()]); Sack.open(String[].class, getFavoritesPath()).commit(urls); } } code of the main collection (exactly the same):
public class ArrayListMain { //private static final String TAG = Log.buildTag(Directory.class); private static List<Station> stations = null; private static Map<String, Station> stationIndex = new HashMap<>(); public static void loadStations(final Context context) { stations = new ArrayList<>(); final AssetManager assetManager = context.getAssets(); String paths[] = new String[0]; try { paths = assetManager.list("stations"); } catch (IOException e) { //Log.e(TAG, "error loading stations", e); return; } for (String path : paths) { if (!path.endsWith(".json")) continue; final Gson gson = new Gson(); Type listType = new TypeToken<ArrayList<Station>>() {}.getType(); List<Station> chunk = null; try { chunk = gson.fromJson(new InputStreamReader(assetManager.open("stations/" + path), "UTF-8"), listType); stations.addAll(chunk); } catch (IOException e) { //Log.e(TAG, "error loading station: %s", e, path); } } Station.sort(stations); indexStations(stations); } private static void indexStations(final List<Station> stations) { stationIndex.clear(); for (Station station : stations) { stationIndex.put(station.getStationUrl(), station); } } public static List<Station> getStations() { return stations; } public static Station getStation(final String url) { return stationIndex.get(url); } }
RecyclerViewcome from? What do theFavorites.getFavorites()andArrayListMain.getStations()methods do? - eugeneekFavorites.getFavorites()method gets objects from the favorites collection. That is, fromArrayList<String>. There is exactly the same collection, just in which there are allList<Station>objects - Anton