顶部悬浮(RecyclerView)

flobberworm.gif

用RecyclerView实现悬浮顶部效果,在网上搜到了很多,有用itemdecoration实现的,这里还是类似
仿美团悬浮效果 的实现方法,只是将addHeaderView改到ViewHolder里了。

xml:




    

        

    

    

        

            
        

        

            
        
    


在Adapter里面加个类型判断,SuspensionViewHolder就是需要有悬浮效果的holder

@Override
public SimpleRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_SUSPENSION) {
            return new SuspensionViewHolder(mInflater.inflate(R.layout.item_suspension, parent, false));
        } else {
            return new SimpleRecycleViewHolder(mInflater.inflate(R.layout.item_recycle_content, parent, false));
        }
}

主要代码还是在滚动监听里做处理,

           @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //获取悬浮框的高度(在onScrolled之前获取均可)
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    suspensionHeight = flSuspension.getHeight();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int firstPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int position = firstPosition + 1;
                if (position >= simpleRecycleAdapter.getItemCount()) {
                    return;
                }
                //当前界面第一个View的类型是否悬浮类型
                if (simpleRecycleAdapter.getItemViewType(position) == TYPE_SUSPENSION) {
                    View view = linearLayoutManager.findViewByPosition(position);
                    if (view != null) {
                        // 下一个itemSuspension 到顶部的距离小于suspensionBar的高度时,通过setY来移动该View
                        if (view.getTop() <= suspensionHeight) {
                            flSuspension.setY(-(suspensionHeight - view.getTop()));
                        } else {
                            //setY(0)固定悬浮View在顶部
                            flSuspension.setY(0);
                        }

                        //解决下滑时悬浮View内容不一致
                        if (dy < 0 && view.getTop() <= suspensionHeight) {
                            for (int i = firstPosition; i >= 0; i--) {
                                if (simpleRecycleAdapter.getItemViewType(i) == TYPE_SUSPENSION) {
                                    updateInfo(i);
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    flSuspension.setY(0);
                }
                //RecycleView至顶时隐藏掉悬浮View,防止致顶时遮挡刷新控件
                flSuspension.setVisibility(linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0 ? View.GONE : View.VISIBLE);
                //当前界面的Position改变后更新
                if (mCurrentPosition != firstPosition) {
                    mCurrentPosition = firstPosition;
                    updateInfo(mCurrentPosition);
                }
            }

你可能感兴趣的:(顶部悬浮(RecyclerView))