即使使用 replace(),导航抽屉片段也会重叠



我在活动中有一个导航抽屉。活动的布局具有片段的相对布局。相对布局的 id 是mainContent,我在活动的onCreate()中加载片段(称之为 fragA)。我在导航抽屉中也有相同的片段以及其他导航项目。它在创建活动时加载,也可以从导航抽屉加载。

在选择导航项时,我将mainContent中的片段(即活动中的相对布局)替换为 replace() 。在导航抽屉中,第 0 项是"fragB"(与 fragA 相同,但在导航抽屉中)。第二个是另一个片段(fragC)。当我"多次"选择碎片B,然后选择碎片A并按后退按钮时,碎片B和碎片C重叠。

而且我还必须继续按后退按钮才能进入初始屏幕,因为每次我从导航抽屉中选择一个项目时,都会创建新的片段碎片B和碎片C。未更换。我在活动布局的mainContent(第一个相对布局)中替换它们。我正在使用FrameLayout作为片段。

这是我在其中放置片段的活动布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.prematixsofs.taxiapp.DateVehiclePicker">
<!-- The main content view -->
<RelativeLayout
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        layout="@layout/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal|bottom"></include>
</RelativeLayout>
<!-- The navigation drawer -->
<RelativeLayout
    android:id="@+id/drawerPane"
    android:layout_width="230dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">
    <!-- Profile Box -->
    <RelativeLayout
        android:id="@+id/profileBox"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#ff02c7c4"
        android:padding="8dp">
        <ImageView
            android:id="@+id/avatar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/user" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/avatar"
            android:orientation="vertical">
            <TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="#fff"
                android:textSize="16sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/viewProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="4dp"
                android:text="View Profile"
                android:textColor="#fff"
                android:textSize="12sp" />
        </LinearLayout>
    </RelativeLayout>
    <!-- List of Actions (pages) -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileBox"
        android:background="#ffffffff"
        android:choiceMode="singleChoice" />
    <TextView
        android:id="@+id/invisibleTextViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</RelativeLayout>

这是我加载 fragA 的活动代码:

 android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.mainContent, new DateTimePicker(),"tags").commit();

这是导航抽屉上的选择:

  private void selectItemFromDrawer(int position) {
    Fragment fragment = null;
    switch (position){
        case 0: fragment = new DateTimePicker();
            break;
        case 1: fragment = new PreferencesFragment();
            break;
        case 2:
            mDrawerLayout.closeDrawer(mDrawerPane);
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(DateVehiclePicker.this);
            alertDialog.setTitle("Logout");
            // Setting Dialog Message
            alertDialog.setMessage("Do you want to Logout?");
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    sessionManager.logoutUser();
                }
            });
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("EXIT", true);
                    startActivity(intent);
                    dialog.cancel();
                }
            });
            alertDialog.show();
    }
    if(fragment!=null)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.addToBackStack(null).replace(R.id.mainContent, fragment);
        fragmentTransaction.commit();
        mDrawerLayout.closeDrawer(mDrawerPane);
    }

这是我为导航抽屉设置侦听器的地方:

 mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItemFromDrawer(position);
        }
    });

我称该方法为selectItemFromDrawer(position);

您必须为此处理 BackStack,您必须清除添加或替换为主片段时堆栈上的所有片段

 @Override
    public void onBackPressed() {
        Logg.e(TAG, getFragmentManager().getBackStackEntryCount());
        try {
            if (mNavigationDrawerFragment.isDrawerOpen())
            {
                mNavigationDrawerFragment.closeDrawer();
            }
            if (getFragmentManager().getBackStackEntryCount() > 1) {
                getFragmentManager().popBackStack();
            } else
            {   Exit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

问题是每次尝试更改片段时,您都会为片段事务创建新对象。在代码中使其全局化,并在任何地方使用相同的实例。这是带有开关大小写的代码,而不是如果其他

 private void displayView(int position) {
            Fragment fragment = null;
            String title = getString(R.string.app_name);
            switch (position) {
                case 0:
                  fragment=new FragmentHome();
                    title="Home";
                    break;
                case 1:
                   fragment=new FragmentAddress();
                    title="Update Address";
                    break;
                case 2:
                    fragment=new FragmentHelpList();
                    title="My Helps";
                    break;
                case 3:
                    fragment=new FragmentCompletedHelps();
                    title="History";
                    break;
                case 4:
                    fragment=new FragmentSupport();
                    title="Account & Support";
                    break;
                case 5:
                    fragment=new FragmentAbout();
                    title="About";
                    break;
                case 6:
                        appSharedPreferences.setSession(false);
                    finish();
                    break;
                default:
                    break;
            }
            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.container_body, fragment);
                fragmentTransaction.commit();
                // set the toolbar title
                getSupportActionBar().setTitle(title);
            }

使片段变量也全局

尝试将 R.id.mainContent 更改为 R.id.container。我有类似的设置,但它在 R.id.container 上工作正常。

fragmentManager.beginTransaction().replace(R.id.container,
                new Registration_fragment()).commit();

并且您最初也必须在加载过程中替换 FragA(而不是添加)。

fragmentTransaction.replace(R.id.mainContent, new DateTimePicker(),"tags").commit();

最新更新