查看寻呼机片段在背面按下时为空白

时间:2016-12-08 10:30:48

标签: android android-fragments android-viewpager

我在片段中使用ViewPager。 viewpager包含两个片段。 当我回到这个片段时,我的问题是我得到了一个空白片段(它只在第一次工作)。

enter image description here

ViewPager代码:

 private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new ListeMagasinsFragment(), getActivity().getString(R.string.liste));
    adapter.addFragment(new MapMagasinsFragment(), getActivity().getString(R.string.carte));
    viewPager.setAdapter(adapter);
}

我也覆盖了onBackPressed():

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            super.onBackPressed();
        } else {
            getSupportFragmentManager().popBackStack();
        }
        //super.onBackPressed();
    }
}

下面的代码显示了片段事务:

 Fragment fragment = new MagasinDetailsFragment();
                    if (fragment != null) {
                        Bundle bundles = new Bundle();

                        if (magasin != null) {
                            bundles.putSerializable("magasin", (Serializable) magasin);
                            Log.e("magasin", "is valid");
                        } else {
                            Log.e("magasin", "is null");
                        }
                        fragment.setArguments(bundles);

                        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.container_body, fragment);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();


                    }

更多代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_magasins, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    viewPager = (ViewPager)view.findViewById(R.id.viewpager);


    setupViewPager(viewPager);

    tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

}

片段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: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="wrap_content"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<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"  />

main_activity.xml:

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    />

 和content_main.xml:

<RelativeLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.carrefour.creova.carrefourmobile.activities.MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
    android:id="@+id/container_body"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在您的Activity中覆盖onBackPressed()方法。

@Override
public void onBackPressed() {

    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else{
 finish();
            }
        }

您必须已将片段添加到BackStack才能使其正常工作,类似于:

Fragment fragment = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putString("variable_if_needed", "Some Value");
    getFragmentManager().beginTransaction()
            .replace(R.id.relativecontent, fragment)
            .addToBackStack(null)
            .commit();
    return true;

“R.id.relativecontent”应该是你的Fragment容器。 是否有必要在按下后退按钮时返回viewPager中的上一页?这不是我经常看到的。例如,上一页(片段)应该是从当前页面向左或向右滑动,您可以实现AlertDialog以提示用户查看是否要退出应用程序。