用RecyclerView实现悬浮顶部效果,在网上搜到了很多,有用itemdecoration实现的,这里还是类似
仿美团悬浮效果 的实现方法,只是将addHeaderView改到ViewHolder里了。
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.flobberworm.demo.recycle.SimpleRecycleActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:minHeight="?attr/actionBarSize"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:lines="1"
android:text="旅游攻略"
android:textColor="@android:color/white"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<FrameLayout
android:id="@+id/fl_suspension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_horizontal"
android:visibility="visible">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:drawableLeft="@mipmap/marutchi"
android:drawablePadding="8dp"
android:gravity="center"
android:text="@string/suspension" />
</FrameLayout>
</RelativeLayout>
</LinearLayout>
在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);
}
}