Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically fetch first result on load more, scrolls the RecyclerView #490

Closed
meness opened this issue Nov 15, 2017 · 6 comments
Closed

Comments

@meness
Copy link

meness commented Nov 15, 2017

Hi,

There's a method to fetch first result on load more automatically, I tested it and I found it scrolls the RecyclerView but it shouldn't.

@davideas
Copy link
Owner

@meness, what do you mean, how many items are loaded, and why you think that it scrolls? I don't see this in the demoApp. I think I really need a video and a way to reproduce it.
Thanks.

@meness
Copy link
Author

meness commented Nov 16, 2017

adapter = new FlexibleAdapter<>(null);
adapter.setEndlessScrollListener(this, new ProgressItem()).setLoadingMoreAtStartUp(true);
@Override
    public void onLoadMore(int lastPosition, int currentPage) {
        presenter.fetchOffProducts(adapter.getItemCount(), 25);
    }

public void productsFetched(List<Product> products) {
        List<IFlexible> items = new ArrayList<>();
        for (Product product : products) {
            items.add(new ProductItem(product));
        }

        adapter.onLoadMoreComplete(items);
    }

@tgivslife
Copy link

Hi @davideas ,

You can reproduce this in the sample app in the FragmentInstagramHeaders class.

Just replace initializeRecyclerView code with

private void initializeRecyclerView() {
        // Initialize Adapter and RecyclerView
        // true = it makes use of stableIds, I strongly suggest to implement 'item.hashCode()'
        FlexibleAdapter.useTag("InstagramHeadersAdapter");
        mAdapter = new FlexibleAdapter<>(new ArrayList<>(), getActivity(), true);
        mAdapter.addListener(getActivity())
                .setAnimationOnScrolling(true)
                .setAnimationOnReverseScrolling(true);
        mRecyclerView = getView().findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(createNewLinearLayoutManager());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true); //Size of RV will not change
        // NOTE: Use default item animator 'canReuseUpdatedViewHolder()' will return true if
        // a Payload is provided. FlexibleAdapter is actually sending Payloads onItemChange.
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        // Custom divider item decorator with 24dpi as empty space between sections
        mRecyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity()).withDefaultDivider());

        mAdapter.setDisplayHeadersAtStartUp(true) //Show Headers at startUp!
                .setStickyHeaders(true) //Make headers sticky
                // Endless scroll with 1 item threshold
                .setEndlessScrollListener(this, new ProgressItem())
				.setLoadingMoreAtStartUp(true)
                .setEndlessScrollThreshold(1); //Default=1

        SwipeRefreshLayout swipeRefreshLayout = getView().findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setEnabled(true);
        mListener.onFragmentChange(swipeRefreshLayout, mRecyclerView, Mode.IDLE);
    }

You init the recycler with 0 items, and setLoadingMoreAtStartUp to true. After the items are loaded the recycler is scrolled a bit down and is not at the first item.

@davideas davideas added the bug label Nov 16, 2017
@davideas
Copy link
Owner

Ok, thanks, I will try to investigate why.

@davideas
Copy link
Owner

An update about the not easy investigation. In onLoadMoreComplete, adding the new items and then hiding the progressItem seems to cause the scroll from RecyclerView, but if I hide before the progressItem, then it seems the scroll doesn't occur. I do not understand why...

You can reproduce the behavior by doing (which are the key calls in the onLoadMoreComplete):

mAdapter.addItems(0, newItems);
mAdapter.removeScrollableFooter(mProgressItem);
//mAdapter.onLoadMoreComplete(newItems, 1000L);

This occurs in main example in endless fragment too, I followed the execution flow, but there's nothing I could identify the cause. But if you invert by the removing the progress item before the adding, then it seems to work as expected.

Now, I need to evaluate the impact of this change. I will come later for a new update.

@davideas
Copy link
Owner

I moved the adding after the removal of the progressItem, this preserves also the feature to delay the removal to display a message in the progressItem.

I release this fix in the new SNAPSHOT in few moments...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants