项目效果:
dragtop_1.1.0.gif
完成的效果如下:
private_detail.gif
- 首先在项目里添加依赖:
dependencies {
compile 'com.github.chenupt.android:dragtoplayout:1.2.1@aar'
}
- 在xml里声明
<github.chenupt.dragtoplayout.DragTopLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--top view-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
...
</LinearLayout>
<!--content view-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>
</github.chenupt.dragtoplayout.DragTopLayout>
- 因为项目需求,不需要topView有下拉缩放的功能,所以,我在代码里设置了 dragLayout.setOverDrag(false);这样下拉的时候,topVIew就不动了。这个功能也可以在xml里设置。项目有个滑动标题栏背景渐变的功能,dragTopLayout提供了很好的监听,可以直接将代码写在监听里,非常简单的完成了背景滑动渐变。
dragLayout.setOverDrag(false);
dragLayout.setCollapseOffset((int) titleHeight)
.listener(new DragTopLayout.SimplePanelListener() {
@Override
public void onSliding(float ratio) {
int alpha = (int) (255 * (1 - ratio));
titleLayout2.setBackgroundColor(Color.argb(alpha, 69, 69, 66));
if (ratio < 0.4) {
avterImg.setVisibility(View.VISIBLE);
tv_smallNickName.setVisibility(View.VISIBLE);
} else {
avterImg.setVisibility(View.GONE);
tv_smallNickName.setVisibility(View.GONE);
}
super.onSliding(ratio);
}
});
- 至于contentView,我采用了TabLayout和ViewPager和Fragment搭配。其中遇到了向上滑,直接就显示topView这个bug。于是我看了例子项目的代码,发现在fragment的ListView的onScroll里采用了EventBus来向宿主页面发送信号,在宿主页面收到信号后调用dragLayout.setTouchMode(boolean)来判断是否拦截touch事件。我采用这个方法之后,bug成功处理。
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
RxBus.getDefault().post(isAdapterViewAttach(listView));
}
public boolean isAdapterViewAttach(AbsListView listView) {
if (listView != null && listView.getChildCount() > 0) {
if (listView.getChildAt(0).getTop() < 0) {
return false;
}
}
return true;
}
- 还遇到了点击topVIew的时候直接显示contentView的不算bug的bug。解决办法是给topVIew添加一个空的点击事件。