use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.
the class QueuePagerFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_queue_pager, container, false);
unbinder = ButterKnife.bind(this, rootView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
if (ShuttleUtils.isLandscape()) {
textProtectionScrim.setVisibility(View.GONE);
}
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(viewModelAdapter);
SnapHelper snapHelper = new PagerSnapHelper() {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
int snapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
if (snapPosition < viewModelAdapter.items.size()) {
Observable.defer(() -> {
if (MusicUtils.getQueuePosition() != snapPosition) {
MusicUtils.setQueuePosition(snapPosition);
}
return Observable.empty();
}).delaySubscription(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).subscribe();
}
return snapPosition;
}
};
snapHelper.attachToRecyclerView(recyclerView);
recyclerView.addOnScrollListener(new RecyclerViewPreloader<>(new ListPreloader.PreloadModelProvider<QueuePagerItemView>() {
@Override
public List<QueuePagerItemView> getPreloadItems(int position) {
QueuePagerItemView queuePagerItemView = (QueuePagerItemView) viewModelAdapter.items.get(position);
return Collections.singletonList(queuePagerItemView);
}
@Override
public GenericRequestBuilder getPreloadRequestBuilder(QueuePagerItemView item) {
return requestManager.load(item.song).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(PlaceholderProvider.getInstance().getPlaceHolderDrawable(item.song.name, true));
}
}, (item, adapterPosition, perItemPosition) -> imageSize, 3));
recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// This null check doesn't make sense to me, but there was an NPE here..
if (recyclerView != null) {
imageSize = new int[] { recyclerView.getWidth(), recyclerView.getHeight() };
recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
return rootView;
}
use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.
the class SearchFragment method onCreate.
@SuppressLint("InlinedApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchPresenter = new SearchPresenter(new PrefixHighlighter(), Glide.with(this));
query = getArguments().getString(ARG_QUERY);
loadingView = new LoadingView();
emptyView = new EmptyView(R.string.empty_search);
emptyView.setHeight(ResourceUtils.toPixels(96));
adapter = new ViewModelAdapter();
}
use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.
the class GridDividerDecoration method getItemOffsets.
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (!(parent.getLayoutManager() instanceof GridLayoutManager)) {
throw new IllegalStateException("GridDividerDecoration can only be used with GridLayoutManager");
}
int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
int position = parent.getChildAdapterPosition(view);
if (position < 0) {
return;
}
int spanIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanIndex(position, spanCount);
int groupIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanGroupIndex(position, spanCount);
RecyclerView.Adapter adapter = parent.getAdapter();
if (adapter instanceof ViewModelAdapter) {
ViewModel viewModel = ((ViewModelAdapter) adapter).items.get(position);
// in which case we add bottom spacing if includeEdge is true)
if (viewModel.getSpanSize(spanCount) == spanCount) {
if (includeEdge && groupIndex == 0) {
outRect.bottom = spacing;
}
return;
}
}
if (includeEdge) {
outRect.left = spacing - spanIndex * spacing / spanCount;
outRect.right = (spanIndex + 1) * spacing / spanCount;
if (groupIndex == 0) {
outRect.top = spacing;
}
outRect.bottom = spacing;
} else {
outRect.left = spanIndex * spacing / spanCount;
outRect.right = spacing - (spanIndex + 1) * spacing / spanCount;
if (groupIndex > 0) {
outRect.top = spacing;
}
}
}
use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.
the class QueuePagerFragment method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModelAdapter = new ViewModelAdapter();
}
use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.
the class GridDividerDecoration method getItemOffsets.
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (!(parent.getLayoutManager() instanceof GridLayoutManager)) {
throw new IllegalStateException("GridDividerDecoration can only be used with GridLayoutManager");
}
int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
int position = parent.getChildAdapterPosition(view);
if (position < 0) {
return;
}
int spanIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanIndex(position, spanCount);
int groupIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanGroupIndex(position, spanCount);
RecyclerView.Adapter adapter = parent.getAdapter();
if (adapter instanceof ViewModelAdapter) {
ViewModel viewModel = ((ViewModelAdapter) adapter).items.get(position);
// in which case we add bottom spacing if includeEdge is true)
if (viewModel.getSpanSize(spanCount) == spanCount) {
if (includeEdge && groupIndex == 0) {
outRect.bottom = spacing;
}
return;
}
}
if (includeEdge) {
outRect.left = spacing - spanIndex * spacing / spanCount;
outRect.right = (spanIndex + 1) * spacing / spanCount;
if (groupIndex == 0) {
outRect.top = spacing;
}
outRect.bottom = spacing;
} else {
outRect.left = spanIndex * spacing / spanCount;
outRect.right = spacing - (spanIndex + 1) * spacing / spanCount;
if (groupIndex > 0) {
outRect.top = spacing;
}
}
}
Aggregations