扩展AppBarLayout时,RecyclerView的高度未调整

时间:2017-07-20 09:14:40

标签: android android-recyclerview android-coordinatorlayout xml-layout

初始化RecyclerView的代码

private void initRecyclerView() {
    layoutManager = new GridLayoutManager(this, spanCount);
    mRecyclerRV.setLayoutManager(layoutManager);
    mRecyclerRV.addItemDecoration(new GridSpacingItemDecoration(spanCount, (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, spaceInDp, getResources().getDisplayMetrics()) + 1), false));
    mRecyclerRV.setNestedScrollingEnabled(false);
    mRecyclerRV.setAdapter(mAdapter);
    mRecyclerRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (!recyclerView.canScrollVertically(-1)
                    && RecyclerView.SCROLL_STATE_SETTLING == newState
                    && mScrollVerticalDirection
                    && mIsAppBarCollapsed) {
                mAppBarLayout.setExpanded(true);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mScrollVerticalDirection = (dy < 0);
        }
    });

    mRecyclerRV.postDelayed(new Runnable() {
        @Override
        public void run() {
            RecyclerView.ViewHolder holder = mRecyclerRV.findViewHolderForAdapterPosition(1);
            if (null != holder) {
                holder.itemView.performClick();
            }
        }
    }, 1000);
}

布局文件在这里,并使用CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_local_album_coordinator"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/colorPageBg">

    <android.support.design.widget.AppBarLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_local_album_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border_bottom_bg"
        android:theme="@style/Wemeet.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/activity_local_album_collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/dp2"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <android.support.v7.widget.Toolbar
                android:id="@+id/local_album_toolbar"
                style="@style/WemeetTitlebar"
                android:layout_marginStart="-10dp"
                android:titleTextColor="@color/colorText33"
                app:layout_scrollFlags="scroll"
                android:theme="@style/Wemeet.Toolbar">

                <TextView
                    android:id="@+id/comm_toolbar_title"
                    style="@style/WemeetTitlebar.Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:text="相机胶卷"
                    android:gravity="center"
                    android:singleLine="true" />

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

            <LinearLayout
                android:layout_gravity="center_horizontal"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_marginTop="@dimen/dp48"
                android:layout_height="350dp"
                app:layout_scrollFlags="scroll">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:background="@color/colorDivider"
                    android:id="@+id/fg_top_border" />

                <com.weiguan.wemeet.basecomm.ui.widget.WemeetVideoView
                    android:visibility="gone"
                    android:layout_gravity="center"
                    android:id="@+id/activity_local_album_preview_video"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                </com.weiguan.wemeet.basecomm.ui.widget.WemeetVideoView>

                <ImageView
                    android:id="@+id/activity_local_album_preview_iv"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitCenter" />
            </LinearLayout>
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/activity_local_album_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

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

折叠AppBarLayout时,RecyclerView滚动正确。

enter image description here

但是当扩展AppBarLayout时,RecyclerView的高度不会调整,滚动显示异常

enter image description here

1 个答案:

答案 0 :(得分:0)

无法获得它。但是,当应用栏展开或崩溃时,请调整回收器视图的大小。