使用FAB与片段

时间:2015-12-28 12:42:11

标签: android xml android-fragments navigation

我试图将FAB与导航抽屉和片段内容一起使用,这些内容随导航菜单中的onitemClcik()而变化

但是在点击任何项目并执行replace()片段之后我得到银行屏幕

课程:

public class SubTaB extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener  , ObservableScrollViewCallbacks  {

        @Bind(R.id.list_view)
        ObservableListView mObservableListView;
        @Bind(R.id.fabtoolbar)
        FabToolbar mFabToolbar;
        @Bind(R.id.fab)
        android.support.design.widget.FloatingActionButton mFab;

        @Bind(R.id.ic_call)
        ImageView mIcCall;
        @Bind(R.id.ic_email)
        ImageView mIcEmail;
        @Bind(R.id.ic_forum)
        ImageView mIcForum;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sub_ta_b);
            final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);


            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open,   R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView)    findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            ButterKnife.bind(this);
            initListView();
            mFabToolbar.setFab(mFab);

        }

        private void initListView() {
            List<String> list = new ArrayList<String>(100);
            for (int i = 0; i < 100; i++) {
                list.add("Item " + i);
            }

         //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  //  android.R.layout.simple_list_item_1, list);
           // mObservableListView.setAdapter(adapter);
            mObservableListView.setScrollViewCallbacks(this);
        }

        @Override
        public void onScrollChanged(int i, boolean b, boolean b1) {

        }

        @Override
        public void onDownMotionEvent() {

        }

        @Override
        public void onUpOrCancelMotionEvent(ScrollState scrollState) {
            Log.d("","Scroll scroll scroll");

            if (scrollState == ScrollState.UP) {
                mFabToolbar.slideOutFab();
            } else if (scrollState == ScrollState.DOWN) {
                mFabToolbar.slideInFab();
            }
        }

        @OnClick(R.id.fab)
        void onFabClick() {
            mFabToolbar.expandFab();
        }

        @OnClick(R.id.call)
        void onClickCall() {
            iconAnim(mIcCall);
        }

        @OnClick(R.id.ic_email)
        void onClickEmail() {
            iconAnim(mIcEmail);
        }

        @OnClick(R.id.ic_forum)
        void onClickForum() {
            iconAnim(mIcForum);
        }

        private void iconAnim(View icon) {
            Animator iconAnim = ObjectAnimator.ofPropertyValuesHolder(
                    icon,
                    PropertyValuesHolder.ofFloat("scaleX", 1f, 1.5f, 1f),
                    PropertyValuesHolder.ofFloat("scaleY", 1f, 1.5f, 1f));
            iconAnim.start();
        }


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

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.sub_ta_b, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }


        @Override
        public boolean onNavigationItemSelected(MenuItem item) {

             //   mFabToolbar.slideOutFab();

            // Handle navigation view item clicks here.
            int id = item.getItemId();
            Fragment newFragment= null;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
       //



            if (id == R.id.nav_camera) {


                newFragment = new OneFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, newFragment);

                fragmentTransaction.commit();


            } else if (id == R.id.nav_gallery) {
                newFragment = new DashDetails();
                transaction.replace(R.id.fragment_container, newFragment);
                transaction.addToBackStack("TAG");
                transaction.commit();
            } else if (id == R.id.nav_slideshow) {

            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
   }

XML layot:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/fragment_container2"
    android:orientation="vertical"
    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/MyMaterialTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/MyMaterialTheme.PopupOverlay" />


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<LinearLayout
    android:id="@+id/fragment_container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        layout="@layout/FAB" />

</LinearLayout>

<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_sub_ta_b"
    app:menu="@menu/activity_sub_ta_b_drawer" />

1 个答案:

答案 0 :(得分:1)

您的活动布局有点奇怪。您应该有一个根父布局,例如CoordinatorLayout。 但是,您的XML布局没有根布局,LinearLayouts都是兄弟。

我注意到的另一件事是FAB周围的LinearLayout是替换片段时替换的那个。这是错的。移除FAB周围的LinearLayout并添加FrameLayout id=fragment_container

布局结构可能如下所示

<CoordinatorLayout>
  <AppBarLayout>
    <Toolbar/>

    //... other layouts here
  </AppBarLayout>

  <FrameLayout 
    android:id="@+id/fragment_container"/>
      //... rest of your layout here

  <FloatingActionButton android:gravity="bottom|end" />
</CoordinatorLayout>