【发布时间】:2016-06-16 05:27:06
【问题描述】:
编辑:完整代码见Using the Master/Detail template in ViewPager Fragments (download link)
我有一个工具栏和一个recyclerView。当布局第一次膨胀时,recyclerView 的最后一项超出了屏幕的可滚动区域,因此不可见。旋转项目后出现。很明显,工具栏正在将recyclerView 推到屏幕边界之外。如果我使用工具栏的高度在recyclerView 的底部添加填充,则问题得到解决,但仅适用于布局的初始膨胀。旋转后,我在屏幕底部留下了一个间隙。我正在使用 SDK 23。
一种可能的解决方法是在布局的第一次膨胀后以编程方式删除填充。我想我可以使用:
onCreateView(){ if (onSaveInstanceState != null) removePadding();}
但是,我宁愿不必做一个狡猾的解决方法。
我的应用程序与 Android Studio 中提供的 Master/Detail-flow 模板基本完全相同,只是我使用片段和单个 Activity。模板中没有这样的问题。
有什么想法吗?
fragment_item_list.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/item_list" />
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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/item_list"
android:name="com.idragonit.scrolltabs.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.idragonit.scrolltabs.ItemListActivity"
tools:listitem="@layout/item_list_content" />
item_list_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
ItemListFragment.java的sn-p
Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
toolbar.setTitle("List");
【问题讨论】:
-
为什么你的行为是在 FrameLayout 而不是 RecyclerView?
-
滚动 RecyclerView 时工具栏是否折叠?
-
没有。工具栏是固定的。只有工具栏。 i67.tinypic.com/o0sdhs.png
-
仅删除此行后tinypic.com/r/33acpzo/9
标签: android android-layout android-fragments android-recyclerview android-toolbar