Search in sources :

Example 1 with InboxMessages

use of me.ccrama.redditslide.Adapters.InboxMessages in project Slide by ccrama.

the class InboxPage method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);
    final RecyclerView rv = v.findViewById(R.id.vertical_content);
    final PreCachingLayoutManager mLayoutManager = new PreCachingLayoutManager(getActivity());
    rv.setLayoutManager(mLayoutManager);
    final SwipeRefreshLayout mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getActivity()));
    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false, Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    posts = new InboxMessages(id);
    adapter = new InboxAdapter(getContext(), posts, rv);
    rv.setAdapter(adapter);
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    // TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter, id, true);
        // TODO catch errors
        }
    });
    rv.addOnScrollListener(new ToolbarScrollHideHandler((getActivity()).findViewById(R.id.toolbar), getActivity().findViewById(R.id.header)) {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = rv.getLayoutManager().getChildCount();
            totalItemCount = rv.getLayoutManager().getItemCount();
            if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
                pastVisiblesItems = ((PreCachingLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
            } else {
                int[] firstVisibleItems = null;
                firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(firstVisibleItems);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    pastVisiblesItems = firstVisibleItems[0];
                }
            }
            if (!posts.loading && !posts.nomore) {
                if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                    posts.loading = true;
                    posts.loadMore(adapter, id, false);
                }
            }
        }
    });
    return v;
}
Also used : InboxAdapter(me.ccrama.redditslide.Adapters.InboxAdapter) CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) RecyclerView(androidx.recyclerview.widget.RecyclerView) InboxMessages(me.ccrama.redditslide.Adapters.InboxMessages) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Aggregations

View (android.view.View)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 InboxAdapter (me.ccrama.redditslide.Adapters.InboxAdapter)1 InboxMessages (me.ccrama.redditslide.Adapters.InboxMessages)1 CatchStaggeredGridLayoutManager (me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager)1 PreCachingLayoutManager (me.ccrama.redditslide.Views.PreCachingLayoutManager)1 ToolbarScrollHideHandler (me.ccrama.redditslide.handler.ToolbarScrollHideHandler)1