Android 滑动RecyclerView时隐藏部分控件

在使用RecyclerView控件时,上下拖动控件时的时候,需要实时的隐藏与显示部分控件,已到达很好的用户体验. 
  原理很简单,当RecyclerView拖动至最上层时显示控件,当RecyclerView.开始向下拖动时,隐藏控件,直接上代码: 
定义两个动画:

private AlphaAnimation mShowAnim, mHiddenAmin;//控件的显示和隐藏动画

初始化:

//控件显示的动画
mShowAnim = new AlphaAnimation(0.0f, 1.0f);
mShowAnim.setDuration(300);

//控件隐藏的动画
mHiddenAmin = new AlphaAnimation(1.0f, 0.0f);
mHiddenAmin.setDuration(300);

RecyclerView控件初始化:

mRecyclerView = (RecyclerView) findViewById(R.id.e_custom_merchandise_activity_recyclerview);
mRecyclerView.addOnScrollListener(new MyRecyclerViewScrollListener());

定义滑动监听事件:

//滑动监听
private class MyRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();//获取最后一个完全显示的ItemPosition
            // 当不滚动时
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                // 判断是否滚动到顶部
                if (lastVisibleItem == 0) {
                    mTextViewPersonalise.startAnimation(mShowAnim);
                    mTextViewPersonalise.setVisibility(View.VISIBLE);
                }
            } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING ) {//拖动中
                if (mTextViewPersonalise.getVisibility() == View.VISIBLE) {
                    mTextViewPersonalise.startAnimation(mHiddenAmin);
                    mTextViewPersonalise.setVisibility(View.INVISIBLE);//注意此处不要使用View.GONE
                }
            }
        }
    }

代码很简单,原理也比较简单.就不上传效果图了!

你可能感兴趣的:(Android 滑动RecyclerView时隐藏部分控件)