Android 碎片';s RelativeLayout->;在活动中的AppBar下(碎片覆盖工具栏)

Android 碎片';s RelativeLayout->;在活动中的AppBar下(碎片覆盖工具栏),android,android-fragments,android-recyclerview,android-relativelayout,Android,Android Fragments,Android Recyclerview,Android Relativelayout,我正在努力解决这个问题 我有带工具栏的活动(2和1选项卡)。但在使用RecyclerView创建Fragment之后,这个RecyclerView只会忽略工具栏,并且在整个屏幕上(全屏/前面) fragment_category.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-aut

我正在努力解决这个问题

我有带工具栏的活动(2和1选项卡)。但在使用RecyclerView创建Fragment之后,这个RecyclerView只会忽略工具栏,并且在整个屏幕上(全屏/前面)

fragment_category.xml

<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:id="@+id/relativelayout_recycler"
    android:layout_height="match_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/spacing_small"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

我知道我只是犯了一些愚蠢的错误,但我不能只是想出来。对不起,我的英语和技能很差。如果您试图帮助我,我将不胜感激:)

您可以将
活动\u main.xml
更改为此

<android.support.design.widget.CoordinatorLayout
    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.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="enterAlways">

            <include layout="@layout/toolbar"/>

        </FrameLayout>

        <View
            android:id="@+id/toolbar_delimiter"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/view_background"
            android:visibility="gone"
            app:layout_scrollFlags="scroll|enterAlways|snap"/>

        <FrameLayout
            android:id="@+id/frame_filters_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:visibility="gone"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <include layout="@layout/toolbar_filters"/>

        </FrameLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

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

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"
        android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/spacing_large"
        android:clickable="true"
        android:src="@drawable/ic_no_item"
        android:tint="@android:color/white"/>

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

您可以将
ViewPager
FloatingActionButton
添加到
CoordinatorLayout


您还可以添加一些
app:layou行为

您是否尝试从头开始重新创建布局?我不认为有必要在协调器布局之外使用RelativeLayout。因为ViewPager和Coordinator布局被设置为完全相同的大小。我不知道您希望得到什么样的输出,但我看到您的
RelativeLayout
的子级正在使用
match_parent
,并且
FAB
父级必须是
CoordinatorLayout
。也就是说,如果需要,则应将
frame\u content
FrameLayout元素替换为ViewPager
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_category, null);
    context = getActivity();
    // activate fragment menu
    setHasOptionsMenu(true);

    db = new DatabaseHandler(getActivity());
    sharedPref = new SharedPref(getActivity());

    resolveCategory();

    lyt_not_found = view.findViewById(R.id.lyt_not_found);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), Tools.getGridSpanCount(getActivity())));
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView v, int state) {
            super.onScrollStateChanged(v, state);
            if(state == RecyclerView.SCROLL_STATE_DRAGGING || state == RecyclerView.SCROLL_STATE_SETTLING){
                ActivityMain.animateFab(true);
            } else {
                ActivityMain.animateFab(false);
            }
        }
    });
    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeResources(
            R.color.colorAccent, R.color.colorAccentDark);
    displayDataFromDatabase();
    return view;
}
<android.support.design.widget.CoordinatorLayout
    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.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="enterAlways">

            <include layout="@layout/toolbar"/>

        </FrameLayout>

        <View
            android:id="@+id/toolbar_delimiter"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/view_background"
            android:visibility="gone"
            app:layout_scrollFlags="scroll|enterAlways|snap"/>

        <FrameLayout
            android:id="@+id/frame_filters_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:visibility="gone"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <include layout="@layout/toolbar_filters"/>

        </FrameLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

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

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"
        android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/spacing_large"
        android:clickable="true"
        android:src="@drawable/ic_no_item"
        android:tint="@android:color/white"/>

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