florent37 / MaterialViewPager

A Material Design ViewPager easy to use library
https://www.fiches-plateau-moto.fr
Apache License 2.0
8.13k stars 1.48k forks source link

How to replace layout viewpagermaterial when use NavigationDrawer #149

Open Noobknight opened 9 years ago

Noobknight commented 9 years ago

First, you've provided a library very useful and I'm thank for that ^^! Today. I'm want to replace viewpager when choose menu in navigation drawer... But don't success. You can help me ? Sorry! I'm speak English not good. Code below <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" >

 <FrameLayout 
    android:id="@+id/frame_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<!-- ViewPager Material  -->
<com.github.florent37.materialviewpager.MaterialViewPager
    android:id="@+id/materialViewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:viewpager_animatedHeaderImage="false"
    app:viewpager_color="@color/colorPrimary"
    app:viewpager_displayToolbarWhenSwipe="true"
    app:viewpager_enableToolbarElevation="true"
    app:viewpager_headerAdditionalHeight="20dp"
    app:viewpager_headerAlpha="1"
    app:viewpager_headerHeight="200dp"
    app:viewpager_hideLogoWithFade="false"
    app:viewpager_hideToolbarAndTitle="false"
    app:viewpager_logoMarginTop="100dp"
    app:viewpager_pagerTitleStrip="@layout/material_view_pager_pagertitlestrip_standard"
    app:viewpager_parallaxHeaderFactor="1.5"
    app:viewpager_transparentToolbar="false" />

<!-- Navigation Drawer -->
<com.github.florent37.materialviewpager.ScrimInsetsRelativeLayout
    android:id="@+id/relativeDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:insetForeground="#4000" >

    <include layout="@layout/navigation_container" />
</com.github.florent37.materialviewpager.ScrimInsetsRelativeLayout>

</android.support.v4.widget.DrawerLayout>

ActivityMain.java

private void setFragmentList(int posicao) {
    mFragmentManager = getSupportFragmentManager();
    switch (posicao) {
    case 1:
                   mFragmentManager.beginTransaction().replace(R.id.frame_content, RecyclerViewFragment.newInstance()).commit();
               break;
    case 2:
         mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(mFragmentManager) {

        @Override
        public Fragment getItem(int position) {
            switch (position % 4) {
                //case 0:
                //    return RecyclerViewFragment.newInstance();
                //case 1:
                //    return RecyclerViewFragment.newInstance();
                //case 2:
                //    return WebViewFragment.newInstance();
                default:
                    return CarpaccioRecyclerViewFragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position % 4) {
                case 0:
                    return "Selection";
                case 1:
                    return "Actualités";
                case 2:
                    return "Professionnel";
                case 3:
                    return "Divertissement";
            }
            return "";
        }
    });
                   mViewPager.getViewPager().setOffscreenPageLimit(
                mViewPager.getViewPager().getAdapter().getCount());
        mViewPager.getPagerTitleStrip().setViewPager(
                mViewPager.getViewPager());
        mNavigationAdapter.resetarCheck();
        mNavigationAdapter.setChecked(posicao, true);
        break;
    }
florent37 commented 9 years ago

The problem is the toolbar, it's inside the materialviewpager :/ I don't know how to fix it properly

florent37 commented 9 years ago

Can you put this layout in a fragment, completely change it with an action from the navigationdrawer ?

bilalilyas90 commented 9 years ago

please can anyone give some example code for drawer item click to change the layout? Thanx in advance

FuriousLlama commented 8 years ago

You can hook up the toolbar directly from the fragments like: `@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { .... AppCompatActivity hostActivity = (AppCompatActivity) getActivity();

    Toolbar toolbar = mViewPager.getToolbar();
    hostActivity.setSupportActionBar(toolbar);
    final ActionBar actionBar = hostActivity.getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setHomeButtonEnabled(true);
    }`

Just have each fragment have its own toolbar except for the materialviewpager one.

jayagautam commented 8 years ago

Hi FuriousLlama,

I am using your material design library. Its quite easy and UI is very attractive.

Presently, i am facing one problem is that, if i want to come back to a fragment from an activity. Then what should i have to do.

Please suggest me.

Thanks in advance.

Jaya Gautam