Fragment的RelativeLayout - >在活动中的AppBar下(片段覆盖工具栏)

时间:2017-08-27 23:18:37

标签: android android-fragments android-recyclerview android-relativelayout

我正在努力解决这个问题。

我有工具栏的活动(2和1 TAB)。但在使用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>

activity_main.xml的一部分

 <RelativeLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        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.design.widget.CoordinatorLayout>

    <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_margin="@dimen/spacing_large"
        android:clickable="true"
        android:src="@drawable/ic_no_item"
        android:tint="@android:color/white" />


</RelativeLayout>

FragmentCategory.java

    @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;
}

我知道我只是犯了一些愚蠢的错误,但我无法弄明白。抱歉我的英语和技能很差。如果你试图帮助我,谢谢你。)

1 个答案:

答案 0 :(得分:1)

您可以将activity_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>

您可以将ViewPagerFloatingActionButton添加到CoordinatorLayout

您可以为其添加一些app:layout_behavior