美文网首页高级UIUI效果仿写自定义控件
android 粗略的 仿京东首页 嵌套方式滚动

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

作者: 一个冬季 | 来源:发表于2020-04-25 15:32 被阅读0次
简单述说

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

仿京东首页.gif
设计思路
简单画图.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

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/main_myframe_behaivor">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/main_header_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>
    <com.example.imitationjd.weiget.MyMainLinLerLayout
        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">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/ctl_first_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                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">
               ...省略部分代码
            </LinearLayout>

            <LinearLayout
                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">
                 ...省略部分代码
            </LinearLayout>

            <LinearLayout
                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">
               ...省略部分代码
            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/first_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.example.imitationjd.weiget.MyMainLinLerLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
public class MainMyLinlayoutBehavior extends CoordinatorLayout.Behavior<MyMainLinLerLayout> {
    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<FrameLayout> {
    ...省略部分代码
    @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;
                }
            }
        }
    }
}

相关文章

网友评论

    本文标题:android 粗略的 仿京东首页 嵌套方式滚动

    本文链接:https://www.haomeiwen.com/subject/fyhkwhtx.html