如何在 Fragment 的 GridView 中滚动时仅隐藏 Tablayout?

How to hide Only Tablayout when scroll in Fragment's GridView?

我需要在 fragment 的 GridView 中滚动时隐藏 Tablayout 和 Filter Layout。 在这里,我试图通过引用此 来仅隐藏 Tablayout,但它不起作用。 如何在滚动时隐藏两个布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/ll_main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    android:orientation="vertical">

        <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/id_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tab_layout_discover"
                    style="@style/AppTabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:elevation="6dp"
                    android:minHeight="?attr/actionBarSize"
                    app:tabTextAppearance="@style/AppTabTextAppearance"
                    app:layout_scrollFlags="scroll|enterAlways"/>
            </android.support.design.widget.CollapsingToolbarLayout>

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

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager_discover"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

这里分享了我的例子

GridView的onScrollListener好像拿不到dxdy。 GridView 的 setOnScrollChangeListener 可以做到,但需要 API 级别 23.

我建议你用RecyclerViewGridLayoutManager代替GridView,使用起来很方便。

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (dy < 0) {
            // hide the layout here
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }
});

我还建议您使用 RecyclerView,因为它具有与旧 Api Versions 兼容的更强大的功能。

要达到预期的效果,您应该这样做:

 rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) {
                Log.d("TAG", "SCROLL Up");// scrolling up
                parentView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        parentTabs.setVisibility(View.VISIBLE);
                    }
                }, 300);

            } else {
                Log.d("TAG", "SCROLL Down");
                parentTabs.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public synchronized void onScrollStateChanged(RecyclerView recyclerView,
                                                      int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                onScroll();
            }
            // If scroll state is touch scroll then set userScrolled
            // true
            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {

                parentView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        parentTabs.setVisibility(View.VISIBLE);
                    }
                }, 1000);

                onIdle();
            }
        }

    });

我想我在这个 post 中解决了类似的情况:

这是关键字:app:layout_scrollFlags="scroll|exitUntilCollapsed" 使用此设置,包含的项目将滚动直到消失。

您可以嵌套两个滚动布局,以便滚动父级中的过滤器而不是容器中的选项卡。

两个布局应该是这样的:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar_layout"
>
<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
>

---- include here everything you want to scroll away ----
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
---- this is your pager or whatever is the content
<include layout="@layout/fragment_pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_below="@+id/app_bar_layout"

/>

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

希望对您有所帮助。

经过参考下面哈伦和穆罕默德的回答,终于实现了这个隐藏/显示动画。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = gridLayoutManager.getChildCount();
                totalItemCount = gridLayoutManager.getItemCount();
                pastVisiblesItems = gridLayoutManager.findFirstVisibleItemPosition();
                if (dy > 0 && !isShow) {
                    isHide = true;
                    view.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            discoverFragment.llFilterTabContatiner.animate().alpha(0).setDuration(500).translationYBy(-discoverFragment.llFilterTabContatiner.getHeight()).setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    discoverFragment.llFilterTabContatiner.setAlpha(1);
                    discoverFragment.llFilterTabContatiner.setVisibility(View.GONE);
                              }
                            });
                        }
                    }, 500);
                } else if (dy < 0 && !isHide) {
                    isShow = true;

                    view.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            discoverFragment.llFilterTabContatiner.setVisibility(View.VISIBLE);
                            discoverFragment.llFilterTabContatiner.setAlpha(1);
                            discoverFragment.llFilterTabContatiner.animate().setDuration(500).translationY(0).setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    discoverFragment.llFilterTabContatiner.setAlpha(1);
                                    isShow = false;
                                }
                            });

                        }
                    }, 500);
                }

                if (pastVisiblesItems == 0 && gridLayoutManager.getChildAt(0) != null && gridLayoutManager.getChildAt(0).getTop() == 0) {
                    swipeRefreshLayout.setEnabled(true);
                } else {
                    swipeRefreshLayout.setEnabled(false);
                }

                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    if (CommonUtil.isConnectingToInternet(context)) {
                        pbViewMore.setVisibility(View.VISIBLE);
                        startPaginationAsync();
                    }
                }
            }
        });