在Recycler View中使用自定义动画进行无限滚动无法使用Android

时间:2015-06-10 19:14:54

标签: android animation android-recyclerview recycler-adapter

我在Recycler视图中使用Wasabeef作为项目动画,它运行正常。我还想为Recycler添加无限滚动,为此我正在做这个

    public abstract class InfiniteScroller extends RecyclerView.OnScrollListener {

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int current_page = 1;

    private LinearLayoutManager mLinearLayoutManager;

    public InfiniteScroller(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }

    public abstract void onLoadMore(int current_page);

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        // Do Nothing
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManager.getItemCount();
        firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }
    }
}

这也很好。真正的挑战是,当我将项目动画与infinte滚动组合时,即使调用了OnScroll侦听器并且还调用了loadMore()并且它正在数据源中添加项目但未在回收器视图中显示它。 Heres是代码。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    mRecylerView = (RecyclerView)view.findViewById(R.id.my_recycler_view);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecylerView.setLayoutManager(mLayoutManager);

    initialize();

    mAdapter = new MyAdapter(persons);

    AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(mAdapter);
    ScaleInAnimationAdapter scaleAdapter = new ScaleInAnimationAdapter(alphaAdapter);
    scaleAdapter.setDuration(1000);
    scaleAdapter.setInterpolator(new OvershootInterpolator());
    mRecylerView.setAdapter(scaleAdapter);

    mRecylerView.setOnScrollListener(new InfiniteScroller(mLayoutManager) {
        @Override
        public void onLoadMore(int current_page) {

            Log.i("Infinite Scroller", "Current page = " + current_page);

            insertNewItem();

        }
    });


    return view;
}

如果我注释掉动画线,它将无限滚动,但不会。 请查看代码,让我知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

所以我自己找到了问题并修复了它:)

对于那些遇到同样问题的人,我会分享我的经验。

我正在调用mAdpter.notifyItemInserted(position)但它应该是scaleAdapter.notifyItemInserted(position)。