滚动到折叠视图而不使用工具栏

时间:2017-12-06 10:49:43

标签: android android-recyclerview android-nestedscrollview nestedscrollview

我正在制作类似于最近android的状态栏的布局。

enter image description here

我有两个Views内部容器。 ViewPagerRecyclerView。 默认行为应该是当我滚动RecyclerView时,我希望ViewPager缩小尺寸,反之亦然。

enter image description here

逻辑:

viewPagerMaxHeight = 200;
if scrollTop
  is ViewPager.height > viewPagerMaxHeight?
    YES: Prevent Scroll and Decrease ViewPager size apropriatry
    No: Scroll RecyclerView
if scrollBottom
  did we scroll to position 0?
    YES: Start increasing ViewPager size
    No: Scroll RecyclerView

很少说明:   - RecyclerView包含各种大小的项目。   - 有时删除并添加项目   - 这是一个简单的RecyclerView,而不是    就像他们互相崩溃的通知一样。

我可以自己构建大部分逻辑,但是我无法为RecyclerView做一个适当的监听器,它将返回滚动的方向和数量。 阻止RecyclerView滚动是一个奖励

修改

我在github

上做了一个例子
v.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        Log.e("scrollY", ""+scrollY);
        Log.e("oldScrollY", ""+oldScrollY);
        Log.e("currentHeight", ""+currentHeight);
        if(scrollY == 200) {
            Log.e("==200", "JU");
        } else if (scrollY < 200) {
            Log.e("<200", ""+currentHeight);

            if(currentHeight < fullHeight) {
                Log.e("current<full", Integer.toString(deltaScroll));
                deltaScroll = oldScrollY - scrollY;
                currentHeight = currentHeight + deltaScroll;
                if(currentHeight > fullHeight) {
                    currentHeight = fullHeight;
                }
                ku.getLayoutParams().height = currentHeight;
                ku.requestLayout();
            }
            v.scrollTo(0, 200);
        } else if (scrollY > oldScrollY) {
            Log.e("Scroll DOWN", "" + Integer.toString(scrollY));
            deltaScroll = scrollY - oldScrollY;
            currentHeight = currentHeight - deltaScroll;
            if(currentHeight > minHeight) {
                ku.getLayoutParams().height = currentHeight;
                ku.requestLayout();
                v.scrollTo(0, 200);

            } else {
                currentHeight = minHeight;
                ku.getLayoutParams().height = minHeight;
                ku.requestLayout();
            }

        }
    }
});

我正在设置RecycleView的填充并将NestedScrollView滚动到第一个项目,因此填充不可见。这样即使已经在TOP上也可以滚动TOP。

一切似乎都有效,但是当你慢慢滚动时你会注意到滚动是“跳跃”(如果滚动得足够快就不会发生)。 我的猜测正在发生,因为NestedScrollView本身会改变高度,例如向上滚动,也会向下滚动。

3 个答案:

答案 0 :(得分:2)

根据您当前的代码:

  

一切似乎都有效,但是当你慢慢滚动时你会注意到滚动是“跳跃的”...

,这是因为当用户缓慢滚动时(由于系统误解为onScrollChange),因此可能会多次调用方法scroll ==> stop ==> scroll ==> stop ==> scroll ==> stop ==>...方法内部的逻辑将被执行多次或/和完全执行,并且由于存在计算和逻辑(ifelse),这可能导致UI的响应性的影响。如果用户将快速滚动它将不会发生,它可能会被调用一次,被解释为单个滚动。

<强> SOLUTION:

  1. 尽可能减少代码中的一些繁琐的计算和逻辑。
  2. 使用Coordinator Layout作为根布局而不是LinearLayout,并在Behaviours内设置您的孩子Views应遵循的CoordinatorLayout。否则使用boolean准备逻辑,这将确保方法onSrollChange的内容不会被调用多次,但只有当用户最终完成整个慢滚动时才会调用一次。

答案 1 :(得分:2)

ConstraintLayoutGuideline的帮助下,上述内容完全可行。因此,当您向上滚动时,以编程方式将指南设置为最小值,当您向下滚动时,将指示线设置回正常。

下面是相同

的xml示例
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">


    <android.support.constraint.Guideline
        android:id="@+id/viewPagerTopGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />


    <android.support.constraint.Guideline
        android:id="@+id/viewPagerBottomGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/viewPagerBottomGuideline"
        app:layout_constraintTop_toBottomOf="@+id/viewPagerTopGuideline">

    </android.support.v4.view.ViewPager>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/viewPagerBottomGuideline"/>

</android.support.constraint.ConstraintLayout>

您也可以将视图放在NestedScrollView内。然后在onscroll上你可以像这样设置指南

 @BindView(R.id.viewPagerBottomGuideline)
Guideline guideLine;//have Used ButterKnife

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
            params.guidePercent = 0.20f; // 45% // range: 0 <-> 1
            guideLine.setLayoutParams(params);

我刚刚在向上滚动时实现了,那么你也必须实现相同的向下滚动。

答案 2 :(得分:1)

我建议您使用CoOrdinate Layout作为主要布局(根布局。)。 那么你需要做的就是通过Java或编码将这种行为设置为折叠工具栏。我已经实现了同样的事情,this链接真的帮了我很多。请点击链接并试一试。

https://github.com/teocci/AndroidGuidenotes/wiki/Handling-Scrolls-with-CoordinatorLayout

相关问题