如何判断VerticalGridView在Android中已滚动到底部?

时间:2015-07-07 04:56:26

标签: android

我正在通过Nexus Player开发适用于Android TV的App。在我的应用中,我使用VerticalGridFragment来播放电视节目。对于节目的数量是2000+,我想在VerticalGridView滚动到底部时使用页面加载。

然而,我不知道如何判断VerticalGridView已滚动到底部。 我使用反射来获取VerticalGridViewaddScrollListener()。这是代码:

   private void addGridScrollListener() {
        try {
            Class<VerticalGridFragment> VerticalGridFragmentClass = VerticalGridFragment.class;
            Field verticalGridViewHolder = VerticalGridFragmentClass.getDeclaredField("mGridViewHolder");
            verticalGridViewHolder.setAccessible(true);
            VerticalGridPresenter.ViewHolder viewHolder = (VerticalGridPresenter.ViewHolder) verticalGridViewHolder.get(this);
            VerticalGridView gridView = viewHolder.getGridView();
            gridView.addOnScrollListener(scrollListener);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }


    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }

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

RecyclerView没有getLastVisiableItemPosition这样的功能,所以我不知道VerticalGridView是否已滚动到底部。

我应该在scrollListener中做什么?或者还有其他解决方案吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

  private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState != RecyclerView.SCROLL_STATE_IDLE) {
            return;
        }

        //get current last child View
        View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount() - 1);
        //get the bottom
        int lastChildBottom = lastChildView.getBottom();
        // get recyclerView's bottom
        int recyclerBottom = recyclerView.getBottom() - recyclerView.getPaddingBottom();
        //get last childview's position
        int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView);


        if (lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) {
            // yes to bottom.
        }
    }
};