android 高仿京东首页,android 粗略的 仿京东首页 嵌套方式滚动

简单述说

最近没有什么事情,就看到了京东的首页,感觉还不错,但是我目前也只是粗略的模仿了出来。

github 地址

V2版本

a8edcfb899e1

仿京东首页.gif

设计思路

a8edcfb899e1

简单画图.png

滚动黄色的recycleView,和viewPager里面的RecyclerView,都需要先确认Frame的位置,如果Frame的translationY == 0了,就只能滚动黄色的recyclerView,如果Frame的translationY == -Frame.getHeight(),就只能滚动viewPager里面的recyclerview了,如果处于-Frame.getHeight() < translationY < 0 都将滚动事件给吃掉,都不让其滚动。这里MyMainLinLerLayout,是继承了LinLayout的,里面实现了 NestedScrollingParent3,当我们滚动viewPager里面的recyclerview的时候,我们会收到 onStartNestedScroll 的回调通知,我们将其返回true,这样最顶层的CoordinatorLayout,就不会收到滚动通知事件了

此次总结

1、如果顶层是CoordinatorLayout,子容器为MyMainLinLerLayout,MyMainLinLerLayout并实现了NestedScrollingParent3,当MyMainLinLerLayout 子容器里面的recyclerview滚动的时候,会通知MyMainLinLerLayout 里面的onStartNestedScroll方法,如果被MyMainLinLerLayout里面的onStartNestedScroll方法拦截了,不会继续将事件返回给CoordinatorLayout里面的onStartNestedScroll

核心代码展示

xml

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:layout_behavior="@string/main_myframe_behaivor">

android:id="@+id/main_header_recyclerview"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/first_my_linlayout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

app:layout_behavior=".behavior.main.MainMyLinlayoutBehavior">

android:id="@+id/ctl_first_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:orientation="horizontal"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent">

...省略部分代码

android:id="@+id/ll_first_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:orientation="horizontal"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toTopOf="parent">

...省略部分代码

android:id="@+id/ll_first_title_desc"

android:layout_width="match_parent"

android:layout_height="@dimen/first_title_desc"

android:gravity="center"

android:orientation="horizontal"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toBottomOf="@+id/ll_first_title">

...省略部分代码

android:id="@+id/first_viewpager"

android:layout_width="match_parent"

android:layout_height="match_parent" />

public class MainMyLinlayoutBehavior extends CoordinatorLayout.Behavior {

private int titleHeight = 0;

@Override

public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull MyMainLinLerLayout child, @NonNull View dependency) {

return dependency instanceof FrameLayout;

}

@Override

public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull MyMainLinLerLayout child, @NonNull View dependency) {

if (titleHeight == 0) {

titleHeight = child.findViewById(R.id.ctl_first_layout).getHeight();

}

FrameLayout frameLayout = (FrameLayout) dependency;

float transY = frameLayout.getHeight() + frameLayout.getTranslationY() / frameLayout.getHeight() * frameLayout.getHeight();

if (transY < 0) {

transY = 0;

}

child.setTranslationY(transY);

return true;

}

}

public class MainMyFrameLayoutBehavior extends CoordinatorLayout.Behavior {

...省略部分代码

@Override

public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FrameLayout child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {

super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);

if (myMainLinLerLayout == null) {

myMainLinLerLayout = coordinatorLayout.findViewById(R.id.first_my_linlayout);

}

if (target instanceof RecyclerView) {

float transY = child.getTranslationY() - dy;

if (target.getId() == R.id.main_header_recyclerview) {

RecyclerView recyclerView = (RecyclerView) target;

mainRecyclerView = recyclerView;

if (dy > 0) {//手指向上

if (recyclerView.canScrollVertically(dy)) {

recyclerView.scrollBy(0, dy);

} else {

if (child.getTranslationY() < -child.getHeight()) {

transY = -child.getHeight();

}

child.setTranslationY(transY);

}

}

if (dy < 0) {//手指向下

if (child.getTranslationY() == 0) {

child.setTranslationY(0);

recyclerView.scrollBy(0, dy);

} else {

if (child.getTranslationY() > -child.getHeight()) {

if (child.getTranslationY() > 0) {

child.setTranslationY(0);

} else {

if (child.getTranslationY() + transY > 0) {

transY = 0;

}

consumed[1] = dy;

child.setTranslationY(transY);

}

if (child.getTranslationY() == 0) {

recyclerView.scrollBy(0, dy);

}

} else {

recyclerView.scrollBy(0, dy);

}

}

}

} else if (target.getId() == R.id.fragment_recyclerview) { //viewpager里面的Recyclerview

if (dy > 0) {//手指向上

if (myMainLinLerLayout.getTranslationY() != 0) {

if (transY < -child.getHeight()) {

transY = -child.getHeight();

}

child.setTranslationY(transY);

consumed[1] = dy;

}

}

if (dy < 0) {//手指向下

if (transY > -child.getHeight() && transY < 0) {

child.setTranslationY(transY);

} else if (transY >0){

if (mainRecyclerView!=null){

mainRecyclerView.scrollBy(0,dy);

}

child.setTranslationY(0);

}

consumed[1] = dy;

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值