use of androidx.paging.PagedList in project AdapterDelegates by sockeqwe.
the class PaginationActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pagination);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
AdapterDelegatesManager<List<DisplayableItem>> delegatesManager = new AdapterDelegatesManager<List<DisplayableItem>>().addDelegate(new AdvertisementAdapterDelegate(this)).addDelegate(new CatAdapterDelegate(this)).addDelegate(new DogAdapterDelegate(this)).addDelegate(new GeckoAdapterDelegate(this)).addDelegate(new SnakeListItemAdapterDelegate(this)).setFallbackDelegate(new LoadingAdapterDelegate(this));
final PagedListDelegationAdapter<DisplayableItem> adapter = new PagedListDelegationAdapter<DisplayableItem>(delegatesManager, callback);
recyclerView.setAdapter(adapter);
LiveData<PagedList<DisplayableItem>> pagedListLiveData = new LivePagedListBuilder<>(new SampleDataSource.Factory(), 20).setBoundaryCallback(new PagedList.BoundaryCallback<DisplayableItem>() {
@Override
public void onZeroItemsLoaded() {
Log.d("PaginationSource", "onZeroItemsLoaded");
super.onZeroItemsLoaded();
}
@Override
public void onItemAtFrontLoaded(@NonNull DisplayableItem itemAtFront) {
Log.d("PaginationSource", "onItemAtFrontLoaded " + itemAtFront);
super.onItemAtFrontLoaded(itemAtFront);
}
@Override
public void onItemAtEndLoaded(@NonNull DisplayableItem itemAtEnd) {
Log.d("PaginationSource", "onItemAtEndLoaded " + itemAtEnd);
super.onItemAtEndLoaded(itemAtEnd);
}
}).build();
pagedListLiveData.observe(this, new Observer<PagedList<DisplayableItem>>() {
@Override
public void onChanged(PagedList<DisplayableItem> displayableItems) {
adapter.submitList(displayableItems);
}
});
}
use of androidx.paging.PagedList in project Hentoid by avluis.
the class LibraryContentFragment method populateAllResults.
private void populateAllResults(@NonNull final PagedList<Content> iLibrary) {
if (null == itemAdapter)
return;
List<ContentItem> contentItems;
if (iLibrary.isEmpty()) {
contentItems = Collections.emptyList();
} else {
@ContentItem.ViewType int viewType;
if (// Grid won't be used in edit mode
Preferences.Constant.LIBRARY_DISPLAY_LIST == Preferences.getLibraryDisplay() || activity.get().isEditMode())
viewType = activity.get().isEditMode() ? ContentItem.ViewType.LIBRARY_EDIT : ContentItem.ViewType.LIBRARY;
else
viewType = ContentItem.ViewType.LIBRARY_GRID;
contentItems = Stream.of(iLibrary.subList(0, iLibrary.size())).withoutNulls().map(c -> new ContentItem(c, touchHelper, viewType, this::onDeleteSwipedBook)).distinct().toList();
}
FastAdapterDiffUtil.INSTANCE.set(itemAdapter, contentItems, CONTENT_ITEM_DIFF_CALLBACK);
new Handler(Looper.getMainLooper()).postDelayed(this::differEndCallback, 150);
}
use of androidx.paging.PagedList in project AndroidRoad by yeungeek.
the class PagingFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recycle_view);
gankViewModel = ViewModelProviders.of(this).get(GankViewModel.class);
gankAdapter = new GankAdapter(new DiffUtil.ItemCallback<GankData>() {
@Override
public boolean areItemsTheSame(@NonNull GankData oldItem, @NonNull GankData newItem) {
return oldItem.get_id() == newItem.get_id();
}
@Override
public boolean areContentsTheSame(@NonNull GankData oldItem, @NonNull GankData newItem) {
return oldItem.getUrl().equals(newItem.getUrl());
}
});
recyclerView.setAdapter(gankAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
gankViewModel.getData().observe(this, new Observer<PagedList<GankData>>() {
@Override
public void onChanged(PagedList<GankData> gankData) {
gankAdapter.submitList(gankData);
}
});
}
use of androidx.paging.PagedList in project Hentoid by avluis.
the class LibraryContentFragment method onLibraryChanged.
/**
* LiveData callback when the library changes
* - Either because a new search has been performed
* - Or because a book has been downloaded, deleted, updated
*
* @param result Current library according to active filters
*/
private void onLibraryChanged(PagedList<Content> result) {
Timber.i(">> Library changed ! Size=%s", result.size());
if (!enabled)
return;
activity.get().updateTitle(result.size(), totalContentCount);
// Update background text
@StringRes int backgroundText = -1;
if (result.isEmpty()) {
if (isSearchQueryActive())
backgroundText = R.string.search_entry_not_found;
else if (0 == totalContentCount)
backgroundText = R.string.downloads_empty_library;
}
if (backgroundText != -1) {
emptyText.setVisibility(View.VISIBLE);
emptyText.setText(backgroundText);
} else
emptyText.setVisibility(View.GONE);
// Update visibility of advanced search bar
activity.get().updateSearchBarOnResults(!result.isEmpty());
String query = getQuery();
// => Suggests searching through all sources except those where the selected book ID is already in the collection
if (newSearch && StringHelper.isNumeric(query)) {
ArrayList<Integer> siteCodes = Stream.of(result).withoutNulls().filter(content -> query.equals(content.getUniqueSiteId())).map(Content::getSite).map(Site::getCode).collect(// ArrayList is required by SearchContentIdDialogFragment.invoke
toCollection(ArrayList::new));
if (!result.isEmpty()) {
Snackbar snackbar = Snackbar.make(recyclerView, R.string.launchcode_present, BaseTransientBottomBar.LENGTH_LONG);
snackbar.setAction(R.string.menu_search, v -> SearchContentIdDialogFragment.invoke(requireContext(), getParentFragmentManager(), query, siteCodes));
snackbar.show();
} else
SearchContentIdDialogFragment.invoke(requireContext(), getParentFragmentManager(), query, siteCodes);
}
// If the update is the result of a new search, get back on top of the list
if (newSearch)
topItemPosition = 0;
// Update displayed books
if (Preferences.getEndlessScroll() && !activity.get().isEditMode() && pagedItemAdapter != null) {
pagedItemAdapter.submitList(result, this::differEndCallback);
} else if (activity.get().isEditMode()) {
populateAllResults(result);
} else {
// Paged mode
if (newSearch)
pager.setCurrentPage(1);
pager.setPageCount((int) Math.ceil(result.size() * 1.0 / Preferences.getContentPageQuantity()));
loadBookshelf(result);
}
// Go back to groups view if there are no books to display (use case : remove the last books from the currently viewed group)
if (result.isEmpty() && Grouping.CUSTOM.equals(Preferences.getGroupingDisplay()))
activity.get().goBackToGroups();
newSearch = false;
library = result;
}
Aggregations