Android RecyclerView OnScroll隐藏布局

时间:2016-02-03 11:07:00

标签: android android-layout android-recyclerview android-toolbar

我试图在我的xml中隐藏回收视图上方的布局..我已经看到隐藏工具栏/操作栏的教程和文档..但我不想这样做...我已经尝试了一些解决方案,但它仍然似乎不能正常工作..

这是我目前的代码。

<LinearLayout

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"
android:orientation="vertical"
>


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_background"
    android:id="@+id/TRIP_LIST_START_LL"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="@dimen/text_margin"
    android:layout_marginRight="@dimen/text_margin"
    android:layout_marginTop="@dimen/text_margin">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/trip_list_iv_flagger"
        android:src="@drawable/flagger"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:layout_marginBottom="25dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Flagger Trip"
        android:id="@+id/trip_list_tv_startflagger"
        android:textSize="@dimen/size22"
        android:layout_gravity="center"
        android:layout_marginLeft="25dp"
        android:textColor="#ce00ee"
        android:textStyle="bold"/>
</LinearLayout>

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/FRAGMENT_TRIP_LIST_SWIPE_REFERESH_LAYOUT"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/FRAGMENT_TRIP_LIST_LIST_VIEW"
        android:name=".Fragments.TripListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:scrollbars="vertical"
        app:layoutManager="LinearLayoutManager"
        tools:context=".Fragments.TripListFragment"
        tools:listitem="@layout/fragment_trip_list_item"/>

</android.support.v4.widget.SwipeRefreshLayout>

 </LinearLayout>

我想要做的就是删除swiperefreshview(recyclerView)上方的布局

在这里隐藏我的布局我在我的scrolllistner上做了什么

      recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        private static final int HIDE_THRESHOLD = 50;
        private int scrolledDistance = 0;
        private boolean layoutVisible = true;

        @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);

            if (scrolledDistance > HIDE_THRESHOLD && layoutVisible) {
                startFlaggerLL.setVisibility(View.GONE);

                layoutVisible = false;
                scrolledDistance = 0;
            } else if (scrolledDistance < -HIDE_THRESHOLD && !layoutVisible) {
                startFlaggerLL.setVisibility(View.VISIBLE);

                layoutVisible = true;
                scrolledDistance = 0;
            }

            if ((layoutVisible && dy > 0) || (!layoutVisible && dy < 0)) {
                scrolledDistance += dy;
            }



        }
    });

我也试过

    if(dy>0)
    {
        System.out.println("Scrolled Downwards");
    }
    else if(dy < 0)
    {
        System.out.println("Scrolled Upwards");

    }

这两个都导致我的布局闪烁..每当我尝试这样做..如果我突然滑动...足够快有时它工作正常并实际隐藏我所需的布局..并显示..

0 个答案:

没有答案