0

我使用Mike Penz的MaterialDrawer实现了一个 MiniDrawer。

它工作正常,但是当我尝试用另一个片段替换框架的内容时,内容会被替换,但旧内容仍保留在那里,并且两个元素都停留在屏幕上。

为了更好地理解,您可以在下面看到图像(红色数字不是屏幕截图的一部分)

在此处输入图像描述

MainActivity.java:

package activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.danish.foveros.R;
import com.mikepenz.crossfader.Crossfader;
import com.mikepenz.crossfader.util.UIUtils;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.MiniDrawer;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import utils.CrossfadeWrapper;


public class MainActivity extends AppCompatActivity{
    Toolbar toolbar;
    private AccountHeader headerResult = null;
    private Drawer result = null;
    private MiniDrawer miniResult = null;
    private Crossfader crossFader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setTitle(R.string.app_name);
        final IProfile profile = new ProfileDrawerItem().withName("MD Danish Ansari").withEmail("ansarid567@gmail.com").withIcon(R.drawable.my_profile);

        headerResult=new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.drawable.header)
                .withTranslucentStatusBar(false)
                .addProfiles(profile)
                .withSavedInstance(savedInstanceState)
                .build();


        DrawerBuilder builder =new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .withTranslucentStatusBar(false)
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.title_home).withIcon(GoogleMaterial.Icon.gmd_home).withIdentifier(1),
                        new PrimaryDrawerItem().withName(R.string.title_favourite).withIcon(GoogleMaterial.Icon.gmd_favorite).withIdentifier(2),
                        new PrimaryDrawerItem().withName(R.string.title_non_material).withIcon(GoogleMaterial.Icon.gmd_color_lens).withIdentifier(3),
                        new PrimaryDrawerItem().withName(R.string.title_web).withIcon(GoogleMaterial.Icon.gmd_web).withIdentifier(4),
                        new DividerDrawerItem(),
                        new PrimaryDrawerItem().withName(R.string.title_about).withIcon(GoogleMaterial.Icon.gmd_info).withIdentifier(5),
                        new PrimaryDrawerItem().withName(R.string.title_contact).withIcon(GoogleMaterial.Icon.gmd_contacts).withIdentifier(6)

                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem iDrawerItem) {
                        if(position==2)
                        {
                            Fragment fragment=new FavouriteFragment();
                            FragmentManager fragmentManager=getSupportFragmentManager();
                            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.content_frame,fragment);
                            fragmentTransaction.commit();
                          result.closeDrawer();
                        }
                        return miniResult.onItemClick(iDrawerItem);
                    }
                })
                .withSavedInstance(savedInstanceState);

        result=builder.buildView();
        miniResult=new MiniDrawer().withDrawer(result).withAccountHeader(headerResult).withInRTL(true);
        //get the widths in px for the first and second panel
        int firstWidth = (int) UIUtils.convertDpToPixel(300, this);
        int secondWidth = (int) UIUtils.convertDpToPixel(72, this);

        //create and build our crossfader (see the MiniDrawer is also builded in here, as the build method returns the view to be used in the crossfader)
        crossFader = new Crossfader()
                .withContent(findViewById(R.id.crossfade_content))
                .withFirst(result.getSlider(), firstWidth)
                .withSecond(miniResult.build(this), secondWidth)
                .withSavedInstance(savedInstanceState)
                .build();

        //define the crossfader to be used with the miniDrawer. This is required to be able to automatically toggle open / close
        miniResult.withCrossFader(new CrossfadeWrapper(crossFader));

        //define a shadow (this is only for normal LTR layouts if you have a RTL app you need to define the other one
        crossFader.getCrossFadeSlidingPaneLayout().setShadowResourceLeft(R.drawable.material_drawer_shadow_left);


    }

}

活动主.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        tools:ignore="UnusedAttribute" />

    <FrameLayout
        android:id="@+id/crossfade_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Hello World"
            android:textSize="32sp" />
    </FrameLayout>
</RelativeLayout>

询问您是否需要更多信息。任何帮助表示赞赏。

4

1 回答 1

1

您的代码显示您有一个FrameLayout托管TextView. FrameLayout用于并且将Crossfader是内容区域。

当您用片段替换内容区域时,您尝试替换不存在的id. 据我所见,content_frame您的布局中没有。

我强烈建议您FrameLayoutcrossfade_content其中放置另一个,稍后将替换为片段。

编辑

fragment_container您应该在没有子代的FrameLayoutid中创建一个crossfade_content,然后您只需设置第一个片段,然后您可以稍后用不同的片段替换这个片段。

所以你会有一些这样的代码:

result = new Drawer()
    .withActivity(this)
    .withToolbar(mToolbar)
    . withFireOnInitialOnClick(true)
    .addDrawerItems(
            new PrimaryDrawerItem().withName("FragmentA").withIdentifier(1),
          new PrimaryDrawerItem().withName("FragmentB").withIdentifier(2)
    )
    .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
        @Override
        public void onItemClick(View view, int position, IDrawerItem drawerItem) {
              if(drawerItem != null) {
                  Fragment f = null;
                  switch(drawerItem.getIdentifier) {
                  case 1:
                    f = FragmentA.newInstance();
                  break;
                  case 2:
                    f = FragmentA.newInstance();

                  break;
                  }

                  if (drawerItem instanceof Nameable) {
                    mToolbar.setTitle(MainActivity.this.getString(((Nameable) drawerItem).getNameRes()));
                  }
              }
            }
        }
    }).build();

这将在启动时设置第一个片段,因为我们设置withFireOnInitialOnClick(true)了将为第一个选定的片段触发。然后用新选定项目的正确片段替换它。onItemClick所以你在监听器中有整个片段逻辑

于 2015-11-01T20:32:58.300 回答