Search in sources :

Example 16 with SwipeRefreshLayout

use of androidx.swiperefreshlayout.widget.SwipeRefreshLayout in project Slide by ccrama.

the class ReaderMode method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    overrideSwipeFromAnywhere();
    super.onCreate(savedInstanceState);
    applyColorTheme("");
    setContentView(R.layout.activity_reader);
    mSubredditColor = getIntent().getExtras().getInt(LinkUtil.EXTRA_COLOR, Palette.getDefaultColor());
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    setupAppBar(R.id.toolbar, "", true, mSubredditColor, R.id.appbar);
    if (getIntent().hasExtra("url")) {
        url = getIntent().getExtras().getString(LinkUtil.EXTRA_URL, "");
        ((Toolbar) findViewById(R.id.toolbar)).setTitle(url);
    }
    v = (SpoilerRobotoTextView) findViewById(R.id.body);
    final SwipeRefreshLayout mSwipeRefreshLayout = ((SwipeRefreshLayout) ReaderMode.this.findViewById(R.id.refresh));
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("", this));
    // 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.SINGLE_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.SINGLE_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    new AsyncGetArticle().execute();
}
Also used : SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) Toolbar(androidx.appcompat.widget.Toolbar)

Example 17 with SwipeRefreshLayout

use of androidx.swiperefreshlayout.widget.SwipeRefreshLayout in project Slide by ccrama.

the class Related method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    overrideSwipeFromAnywhere();
    super.onCreate(savedInstanceState);
    applyColorTheme("");
    setContentView(R.layout.activity_search);
    Intent intent = getIntent();
    if (intent.hasExtra(Intent.EXTRA_TEXT) && !intent.getExtras().getString(Intent.EXTRA_TEXT, "").isEmpty()) {
        url = intent.getStringExtra(Intent.EXTRA_TEXT);
    }
    if (intent.hasExtra(EXTRA_URL)) {
        url = intent.getStringExtra(EXTRA_URL);
    }
    if (url == null || url.isEmpty()) {
        new AlertDialog.Builder(this).setTitle("URL is empty").setMessage("Try again with a different link!").setCancelable(false).setPositiveButton(R.string.btn_ok, (dialogInterface, i) -> finish()).show();
    }
    setupAppBar(R.id.toolbar, "Related links", true, true);
    // it won't be, trust me
    assert mToolbar != null;
    mToolbar.setPopupTheme(new ColorPreferences(this).getFontStyle().getBaseId());
    final RecyclerView rv = ((RecyclerView) findViewById(R.id.vertical_content));
    final PreCachingLayoutManager mLayoutManager = new PreCachingLayoutManager(this);
    rv.setLayoutManager(mLayoutManager);
    rv.addOnScrollListener(new ToolbarScrollHideHandler(mToolbar, 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 && (visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                posts.loading = true;
                posts.loadMore(adapter, "", "url:" + url, false);
            }
        }
    });
    final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("", this));
    // 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.SINGLE_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.SINGLE_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    posts = new SubredditSearchPosts("", "url:" + url, this, false);
    adapter = new ContributionAdapter(this, posts, rv);
    rv.setAdapter(adapter);
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    // TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter, "", "url:" + url, true);
        // TODO catch errors
        }
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) R(me.ccrama.redditslide.R) NonNull(androidx.annotation.NonNull) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) Intent(android.content.Intent) Constants(me.ccrama.redditslide.Constants) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) Palette(me.ccrama.redditslide.Visuals.Palette) SubredditSearchPosts(me.ccrama.redditslide.Adapters.SubredditSearchPosts) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) MenuItem(android.view.MenuItem) ContributionAdapter(me.ccrama.redditslide.Adapters.ContributionAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) Intent(android.content.Intent) SubredditSearchPosts(me.ccrama.redditslide.Adapters.SubredditSearchPosts) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ContributionAdapter(me.ccrama.redditslide.Adapters.ContributionAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 18 with SwipeRefreshLayout

use of androidx.swiperefreshlayout.widget.SwipeRefreshLayout in project Slide by ccrama.

the class ModLog method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);
    RecyclerView rv = v.findViewById(R.id.vertical_content);
    final PreCachingLayoutManager mLayoutManager = new PreCachingLayoutManager(getActivity());
    rv.setLayoutManager(mLayoutManager);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("mod", 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 ModLogPosts();
    adapter = new ModLogAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);
    rv.addOnScrollListener(new ToolbarScrollHideHandler(((ModQueue) getActivity()).mToolbar, (getActivity()).findViewById(R.id.header)));
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter);
        }
    });
    return v;
}
Also used : ModLogAdapter(me.ccrama.redditslide.Adapters.ModLogAdapter) ModQueue(me.ccrama.redditslide.Activities.ModQueue) ModLogPosts(me.ccrama.redditslide.Adapters.ModLogPosts) RecyclerView(androidx.recyclerview.widget.RecyclerView) 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)

Example 19 with SwipeRefreshLayout

use of androidx.swiperefreshlayout.widget.SwipeRefreshLayout in project Slide by ccrama.

the class MultiredditView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);
    rv = v.findViewById(R.id.vertical_content);
    final RecyclerView.LayoutManager mLayoutManager = createLayoutManager(LayoutUtils.getNumColumns(getResources().getConfiguration().orientation, getActivity()));
    rv.setLayoutManager(mLayoutManager);
    if (SettingValues.fab) {
        fab = v.findViewById(R.id.post_floating_action_button);
        if (SettingValues.fabType == Constants.FAB_POST) {
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    final ArrayList<String> subs = new ArrayList<>();
                    for (MultiSubreddit s : posts.multiReddit.getSubreddits()) {
                        subs.add(s.getDisplayName());
                    }
                    new MaterialDialog.Builder(getActivity()).title(R.string.multi_submit_which_sub).items(subs).itemsCallback(new MaterialDialog.ListCallback() {

                        @Override
                        public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                            Intent i = new Intent(getActivity(), Submit.class);
                            i.putExtra(Submit.EXTRA_SUBREDDIT, subs.get(which));
                            startActivity(i);
                        }
                    }).show();
                }
            });
        } else if (SettingValues.fabType == Constants.FAB_SEARCH) {
            fab.setImageResource(R.drawable.ic_search);
            fab.setOnClickListener(new View.OnClickListener() {

                String term;

                @Override
                public void onClick(View v) {
                    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity()).title(R.string.search_title).alwaysCallInputCallback().input(getString(R.string.search_msg), "", new MaterialDialog.InputCallback() {

                        @Override
                        public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
                            term = charSequence.toString();
                        }
                    });
                    builder.positiveText(getString(R.string.search_subreddit, "/m/" + posts.multiReddit.getDisplayName())).onPositive(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                            Intent i = new Intent(getActivity(), Search.class);
                            i.putExtra(Search.EXTRA_TERM, term);
                            i.putExtra(Search.EXTRA_MULTIREDDIT, posts.multiReddit.getDisplayName());
                            startActivity(i);
                        }
                    });
                    builder.show();
                }
            });
        } else {
            fab.setImageResource(R.drawable.ic_visibility_off);
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(false);
                        }).show();
                    } else {
                        clearSeenPosts(false);
                    }
                }
            });
            fab.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(true);
                        }).show();
                    } else {
                        clearSeenPosts(true);
                    }
                    /*
                        ToDo Make a sncakbar with an undo option of the clear all
                        View.OnClickListener undoAction = new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                adapter.dataSet.posts = original;
                                for(Submission post : adapter.dataSet.posts){
                                    if(HasSeen.getSeen(post.getFullName()))
                                        Hidden.undoHidden(post);
                                }
                            }
                        };*/
                    Snackbar s = Snackbar.make(rv, getResources().getString(R.string.posts_hidden_forever), Snackbar.LENGTH_LONG);
                    LayoutUtils.showSnackbar(s);
                    return false;
                }
            });
        }
    } else {
        v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    }
    refreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    /**
     * If using List view mode, we need to remove the start margin from the SwipeRefreshLayout.
     * The scrollbar style of "outsideInset" creates a 4dp padding around it. To counter this,
     * change the scrollbar style to "insideOverlay" when list view is enabled.
     * To recap: this removes the margins from the start/end so list view is full-width.
     */
    if (SettingValues.defaultCardView == CreateCardView.CardEnum.LIST) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        MarginLayoutParamsCompat.setMarginStart(params, 0);
        rv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        refreshLayout.setLayoutParams(params);
    }
    List<MultiReddit> multireddits;
    if (profile.isEmpty()) {
        multireddits = UserSubscriptions.multireddits;
    } else {
        multireddits = UserSubscriptions.public_multireddits.get(profile);
    }
    if ((multireddits != null) && !multireddits.isEmpty()) {
        refreshLayout.setColorSchemeColors(Palette.getColors(multireddits.get(id).getDisplayName(), getActivity()));
    }
    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we estimate the height of the header in dp
    refreshLayout.setProgressViewOffset(false, Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    refreshLayout.post(new Runnable() {

        @Override
        public void run() {
            refreshLayout.setRefreshing(true);
        }
    });
    if ((multireddits != null) && !multireddits.isEmpty()) {
        posts = new MultiredditPosts(multireddits.get(id).getDisplayName(), profile);
        adapter = new MultiredditAdapter(getActivity(), posts, rv, refreshLayout, this);
        rv.setAdapter(adapter);
        rv.setItemAnimator(new SlideUpAlphaAnimator().withInterpolator(new LinearOutSlowInInterpolator()));
        posts.loadMore(getActivity(), this, true, adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                posts.loadMore(getActivity(), MultiredditView.this, true, adapter);
            // TODO catch errors
            }
        });
        if (fab != null) {
            fab.show();
        }
        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();
                int[] firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(null);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    for (int firstVisibleItem : firstVisibleItems) {
                        pastVisiblesItems = firstVisibleItem;
                        if (SettingValues.scrollSeen && pastVisiblesItems > 0 && SettingValues.storeHistory) {
                            HasSeen.addSeenScrolling(posts.posts.get(pastVisiblesItems - 1).getFullName());
                        }
                    }
                }
                if (!posts.loading) {
                    if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount && !posts.nomore) {
                        posts.loading = true;
                        posts.loadMore(getActivity(), MultiredditView.this, false, adapter);
                    }
                }
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
                    diff += dy;
                } else {
                    diff = 0;
                }
                if (fab != null) {
                    if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {
                        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING || diff < -fab.getHeight() * 2)
                            fab.show();
                    } else {
                        fab.hide();
                    }
                }
            }
        });
    }
    return v;
}
Also used : LayoutUtils(me.ccrama.redditslide.util.LayoutUtils) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) SettingValues(me.ccrama.redditslide.SettingValues) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) MultiSubreddit(net.dean.jraw.models.MultiSubreddit) R(me.ccrama.redditslide.R) HasSeen(me.ccrama.redditslide.HasSeen) NonNull(androidx.annotation.NonNull) Submission(net.dean.jraw.models.Submission) Intent(android.content.Intent) Constants(me.ccrama.redditslide.Constants) Hidden(me.ccrama.redditslide.Hidden) ArrayList(java.util.ArrayList) Locale(java.util.Locale) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) Fragment(androidx.fragment.app.Fragment) Search(me.ccrama.redditslide.Activities.Search) View(android.view.View) UserSubscriptions(me.ccrama.redditslide.UserSubscriptions) RecyclerView(androidx.recyclerview.widget.RecyclerView) AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) MultiReddit(net.dean.jraw.models.MultiReddit) LayoutInflater(android.view.LayoutInflater) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) MarginLayoutParamsCompat(androidx.core.view.MarginLayoutParamsCompat) Reddit(me.ccrama.redditslide.Reddit) MultiredditAdapter(me.ccrama.redditslide.Adapters.MultiredditAdapter) DialogAction(com.afollestad.materialdialogs.DialogAction) Submit(me.ccrama.redditslide.Activities.Submit) Palette(me.ccrama.redditslide.Visuals.Palette) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) ViewGroup(android.view.ViewGroup) CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) SubmissionDisplay(me.ccrama.redditslide.Adapters.SubmissionDisplay) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) List(java.util.List) Configuration(android.content.res.Configuration) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) RelativeLayout(android.widget.RelativeLayout) MultiredditPosts(me.ccrama.redditslide.Adapters.MultiredditPosts) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Snackbar(com.google.android.material.snackbar.Snackbar) ArrayList(java.util.ArrayList) MultiSubreddit(net.dean.jraw.models.MultiSubreddit) MultiReddit(net.dean.jraw.models.MultiReddit) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) Search(me.ccrama.redditslide.Activities.Search) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Intent(android.content.Intent) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) MultiredditPosts(me.ccrama.redditslide.Adapters.MultiredditPosts) DialogAction(com.afollestad.materialdialogs.DialogAction) RelativeLayout(android.widget.RelativeLayout) Submit(me.ccrama.redditslide.Activities.Submit) RecyclerView(androidx.recyclerview.widget.RecyclerView) MultiredditAdapter(me.ccrama.redditslide.Adapters.MultiredditAdapter) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 20 with SwipeRefreshLayout

use of androidx.swiperefreshlayout.widget.SwipeRefreshLayout in project Slide by ccrama.

the class HistoryView 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(getContext());
    rv.setLayoutManager(mLayoutManager);
    rv.setItemViewCacheSize(2);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("default", 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 HistoryPosts();
    adapter = new ContributionAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    // TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter, 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) {
                if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount && !posts.nomore) {
                    posts.loading = true;
                    posts.loadMore(adapter, false);
                }
            }
        }
    });
    return v;
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) ContributionAdapter(me.ccrama.redditslide.Adapters.ContributionAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) HistoryPosts(me.ccrama.redditslide.Adapters.HistoryPosts) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Aggregations

SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)41 RecyclerView (androidx.recyclerview.widget.RecyclerView)20 View (android.view.View)19 ToolbarScrollHideHandler (me.ccrama.redditslide.handler.ToolbarScrollHideHandler)10 Toolbar (androidx.appcompat.widget.Toolbar)9 FastScroller (eu.davidea.fastscroller.FastScroller)8 FlexibleItemDecoration (eu.davidea.flexibleadapter.common.FlexibleItemDecoration)8 CatchStaggeredGridLayoutManager (me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager)8 PreCachingLayoutManager (me.ccrama.redditslide.Views.PreCachingLayoutManager)8 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)7 ExampleAdapter (eu.davidea.samples.flexibleadapter.ExampleAdapter)7 ScrollableUseCaseItem (eu.davidea.samples.flexibleadapter.items.ScrollableUseCaseItem)7 Bundle (android.os.Bundle)6 Intent (android.content.Intent)5 Handler (android.os.Handler)5 TextView (android.widget.TextView)5 ContributionAdapter (me.ccrama.redditslide.Adapters.ContributionAdapter)5 Context (android.content.Context)4 NonNull (androidx.annotation.NonNull)4 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)4