Android:当滚动到达结尾时如何隐藏按钮

时间:2017-12-21 13:29:09

标签: android layout android-coordinatorlayout

我的布局包含顶部的scrollview和屏幕底部的按钮。当我向下滚动时我想隐藏这个按钮,当我到达滚动视图结束时显示它。我怎么能意识到这一点?

红色 - ButtonScrollView - 蓝色。

1 个答案:

答案 0 :(得分:3)

试试这个 的 main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Submit" />

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

活动类

    RecyclerView mRecyclerView = findViewById(R.id.recycler_view);
    Button mSubmit = findViewById(R.id.submit);


    mRecyclerView .addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                         //dx horizontal distance scrolled in pixels
                         //dy vertical distance scrolled in pixels
                        super.onScrolled(recyclerView, dx, dy);

                        if (dy > 0 && mSubmit.getVisibility() == View.VISIBLE) {
                            mSubmit.setVisibility(View.GONE);
                        } else if (dy < 0 && mSubmit.getVisibility() != View.VISIBLE) {
                            mSubmit.setVisibility(View.VISIBLE);
                        }
                    }
                });