1

当用户到达子类NestedScrollView中的某个视图时,我需要更新我的选项卡(TabLayout ) 。

我的布局:

<android.support.design.widget.CoordinatorLayout>

    <!-- My Subclassed NestedScrollView. This is due to parallax image on top .-->
    <com.company.recipes.ObservableScrollView>

        <!-- Container for parallax image -->
        <FrameLayout>
            <ImageView/>
        </FrameLayout>
        <!-- Some Content -->
        <View 
           android:id="@+id/divider"/>
        <!-- More Content -->
        <View 
           android:id="@+id/divider2"/>
    </com.company.recipes.ObservableScrollView>

    <android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.Toolbar
           android:id="@+id/toolbar_detail"
           app:layout_scrollFlags="scroll|enterAlways"/>
        <android.support.design.widget.TabLayout
           android:id="@+id/tab_layout"/>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

为了更清楚地说明我的意思,这是一张带有实际数据的图像:

带数据的图像

TabLayout 有 3 个固定选项卡。TabLayout 始终停留在屏幕上。当用户向下或向上滚动时,我想更新 TabIndicator。当它通过 Divider 时,应该会发生这种情况。

那么如何制作一个滚动监听器,它会发现视图是否已经滚动到可见区域之外?

这是我的子类 NestedScrollView: ObservableScrollView.java

public class ObservableScrollView extends NestedScrollView {

    public interface OnScrollChangedListener {
        void onScrollChanged(int deltaX, int deltaY);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(l - oldl, t - oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }
}

我的 onScrollChangedListener 处理视差图像滚动:

final ObservableScrollView scrollView = (ObservableScrollView) findViewById(R.id.myscrollView);
scrollView.setOnScrollChangedListener(new ObservableScrollView.OnScrollChangedListener() {
                @Override
                public void onScrollChanged(int deltaX, int deltaY) {
                    int scrollY = scrollView.getScrollY();
                    // Add parallax effect
                    findViewById(R.id.frame_photo).setTranslationY(scrollY * 0.5f);
                }
            });

它指的是这个SO question。

4

0 回答 0