我正在使用MainActivity根据底部导航栏(例如frgament_list_layout)在片段之间移动。在那里,我有一个带有可单击项的Recycleview,单击时我想在当前片段的顶部打开一个新片段,但它在我的导航栏上运行(我希望与以前保持相同)。 你可以帮我吗?
我想设置一个新片段,但不知道为什么它不起作用。
public class ListOfBooks extends Fragment {
...
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_layout, null);
..
}
...
/** this function replaces the layout to a book page layout in case some book was clicked in the list */
private void loadBookPageFragment() {
BookPageFragment bookPage = new BookPageFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction frag_trans = getActivity().getSupportFragmentManager().beginTransaction();;
frag_trans = manager.beginTransaction();
frag_trans.addToBackStack("ListView"); // enables to press "return" and go back to the list view
frag_trans.hide(ListOfBooks.this);
frag_trans.replace(android.R.id.content, bookPage);
frag_trans.commit();
}
}
class BookPageFragment extends Fragment
设置fragment_book_page.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"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="@string/book_name" >
<ImageView
android:id="@+id/toolbar_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_temp_icon" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="parallax"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
layout="@layout/content_scrolling" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/btn_star" />
</android.support.design.widget.CoordinatorLayout>
我的导航栏:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_list"
android:icon="@mipmap/ic_map_marker_radius"
android:title="@string/title_list" />
<item
android:id="@+id/navigation_map_view"
android:icon="@mipmap/ic_map_marker"
android:title="@string/title_map_view" />
<item
android:id="@+id/navigation_profile"
android:icon="@mipmap/ic_profile_black"
android:title="@string/title_profile" />
</menu>
``