到达RecyclerView结束时删除FAB

时间:2016-01-04 10:44:41

标签: android android-layout

我正在尝试创建一个屏幕,其中有一个带有固定数量元素(4个元素)和FAB按钮(右下角位置)的Recycler View。

当我向下滑动并到达RecyclerView的最后一个元素时,我想删除FAB按钮(翻译或显示效果)。

当我在达到目的地后向上滚动时,我还想让FAB再次回来。

请你告诉我怎么做。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

addOnScrollListener添加到RecyclerView

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

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    mOnScreenItems = mRecyclerView.getChildCount();
    mTotalItemsInList = linearLayoutManager.getItemCount();
    mFirstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
    if (mLoadingItems) {
        if (mTotalItemsInList > mPreviousTotal+1) {
            mLoadingItems = false;
            mPreviousTotal = mTotalItemsInList;
        }
    }
    if (!mLoadingItems && (mTotalItemsInList - mOnScreenItems) <= (mFirstVisibleItem + mVisibleThreshold)) {
        // end reached
        mLoadingItems = true;
    }
}
});

答案 1 :(得分:0)

LinearLayoutManager layoutManager=new LinearLayoutManager(this);
//Add this to your Recyclerview
recyclerview.setLayoutManager(layoutManager);

//Tocheck if  recycler is on top
if(layoutManager.firstCompletelyVisibleItemPosition()==0){
//Its at top ..
fab.show();
}

//Tocheck if  recycler is on bottom
    if(layoutManager.lastCompletelyVisibleItemPosition()==data.size()-1){
    //Its at bottom ..
     fab.hide();
    }