导航抽屉片段甚至与replace()重叠

时间:2016-01-28 11:27:09

标签: android android-layout android-fragments

我在活动中有一个导航抽屉。活动的布局具有片段的相对布局。 relativelayout的id为mainContent,我在其中加载活动的onCreate()中的片段(称为fragA)。我在导航抽屉中也有这个相同的片段以及其他导航项。它加载了oncreate of activity,也可以从导航抽屉中加载。

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

而且我还必须按下后退按钮才能进入初始屏幕,因为每次我从导航抽屉中选择一个项目时,都会创建新的碎片fragB和fragCs。没有更换。我在活动布局中的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();

这是导航抽屉onSelect:

  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);

3 个答案:

答案 0 :(得分:0)

问题是您是否每次都在为片段事务创建新对象您正在尝试更改片段。在代码中使其全局化并在任何地方使用相同的实例。 这是带有switch case的代码,而不是if else

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

使片段变量也是全局的

答案 1 :(得分:0)

你必须为此处理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();
        }
    }

答案 2 :(得分:0)

尝试将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();