【发布时间】:2015-07-29 19:07:12
【问题描述】:
我正在尝试将协调器布局与将片段托管为“滚动视图”的 appbar 布局一起使用。该片段由一个 recyclerView 和一个底部对齐的布局组成,其中包含一个按钮,如下所示:
但是,底部默认是隐藏的:
只有在我滚动后才会显示。
来自我的活动课:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestFragment fragment = (TestFragment) getFragmentManager().findFragmentByTag("Test");
if (fragment == null)
fragment = new TestFragment();
getFragmentManager().popBackStack();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment, "Test");
fragmentTransaction.commit();
}
活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="enterAlways|scroll"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
片段布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottomView"
android:scrollbars="vertical"
android:visibility="visible"/>
<RelativeLayout
android:id="@+id/bottomView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#d4285d"
app:layout_scrollFlags="enterAlways">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do something..."
android:textSize="14sp"/>
</RelativeLayout>
</RelativeLayout>
这里的最终目标是让片段的底部栏始终显示在屏幕上,并且回收站视图滚动应用栏。
这可能吗?
谢谢大家!
【问题讨论】:
-
我也有同样的问题。我通过将 'bottomLayout' 放在 Activity(而不是 Fragment)和 CoordinatorLayout 之外来解决了一半。即使在滚动时它现在仍然可见。但是,我有时也想隐藏它(不是在滚动时,而是在我按下工具栏按钮时),在这种情况下,滚动视图(RecyclerView)不会填满留下的空间,而是保持“空”(白色的)。此外,“bottomView”出现在滚动视图的顶部,即使它位于 CoordinatorLayout 之外并且位于 LinearLayout 的下方。我很困惑...
-
Nick,我最终暂时采用了相同的方法,将bottomLayout放入Activity。我通过向 Activity 添加 FrameLayout 以通用方式处理此问题,我从片段访问它并在其中添加一个视图。它很乱,但我无法绕过它。我有时不必处理隐藏那个栏(除了在我切换片段时完全隐藏它)所以我不能评论这个问题......对不起。
标签: android android-fragments appbar android-coordinatorlayout coordinator-layout