当recyclerview有一个项目时,防止工具栏隐藏

时间:2015-06-20 19:24:18

标签: android toolbar android-recyclerview android-design-library

我有一些问题。当recyclerview具有少量项目(1,2,3,4)时,工具栏将隐藏。这是不受欢迎的行为。

如果在recyclerview只有少量项目并且在recyclerview上没有滚动时,如何防止工具栏隐藏?

main_recyclerview.xml

GIF showing the problem     

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="1dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
     >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="1dp"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways"
        />


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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >


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

        </android.support.v7.widget.RecyclerView>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:elevation="1dp"
    android:onClick="addCity"
    android:src="@drawable/ic_plus_white_36dp"
    app:borderWidth="0dp" />

1 个答案:

答案 0 :(得分:1)

可能您需要阻止RecyclerView将滚动事件分派给其父母。

     private class NoScrollTouchListener implements RecyclerView.OnTouchListener{
            private static final int MAX_CLICK_DURATION = 200;
            private long mStartClickTime;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mStartClickTime = SystemClock.currentThreadTimeMillis();
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        long clickDuration = SystemClock.currentThreadTimeMillis() - mStartClickTime;
                        if(clickDuration <= MAX_CLICK_DURATION) {
                            return false;
                        }
                    }
                }
                if(v instanceof RecyclerView){
                    boolean isLastVisible = ((LinearLayoutManager)((RecyclerView) v).getLayoutManager()).findLastCompletelyVisibleItemPosition() == ((RecyclerView) v).getAdapter().getItemCount() - 1);

   boolean isFirstVisible =  ((LinearLayoutManager)((RecyclerView) v).getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0);
                   return isLastVisible && isFirstVisible;
                }
                return true;
            }
        }

这可能不是正确的方法,但它在RecyclerView不需要滚动时具有简洁的条件。

((LinearLayoutManager)mRecyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() - 1);

另请参阅RecyclerView parent的requestDisallowInterceptTouchEvent(disallow)(即CoordinatorLayout

尝试阻止任何滚动行为的另一种方法是将数据观察者注册到RecyclerView.Adapter并使用上述条件切换layout_scrollFlags的{​​{1}}:

Toolbar

当您将滚动标记设置为0时,AppBarLayout将排除该视图在总灵活范围内的计算。