片段没有正确替换

时间:2015-11-01 20:12:25

标签: android android-fragments material-design materialdrawer

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

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

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

enter image description here

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


    }

}

activity_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: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>

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

1 个答案:

答案 0 :(得分:1)

您的代码显示您有FrameLayout来托管TextViewFrameLayout用于Crossfader,将成为内容区域。

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

我强烈建议您在FrameLayout中放置另一个crossfade_content,稍后将替换该片段。

修改

您应该在fragment_container内创建一个FrameLayout,其ID为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监听器

中拥有整个片段逻辑