0

I have added coordinatorlayout+viewpager+TabLayout and have added three tabs with viewpager but scrolling only works with first tab(recent)

not working with two tabs 1. contact,2.setting

see all codes only xml code posted here as only needed

homeactivity xml(where three fragment get attached)

<android.support.design.widget.CoordinatorLayout 
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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.HomeActivity">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="6dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:elevation="0dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/white"
        app:tabMode="fixed"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextColor="@color/white" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tab_layout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

contact_fragment.xml(with swiperefreshlayout)

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- place your view here -->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_registered"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="2dp"
            android:background="@color/black" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_invite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:scrollbars="vertical" />
    </LinearLayout>


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

setting_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

</RelativeLayout>

HOMEACTIVITY CODE

public void BindView() {
    appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("RECENT");
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    TabLayout tabLayout;
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setSelected(true);
    viewPager.setCurrentItem(0);


    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()) {
                case 0:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("RECENT");
                    break;
                case 1:
                    viewPager.setCurrentItem(1);
                    toolbar.setTitle("CONTACT");
                    break;
                case 2:
                    viewPager.setCurrentItem(2);
                    toolbar.setTitle("SETTING");
                    break;
                default:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("RECENT");
                    break;
            }
            /*if (viewPager.getCurrentItem() == 0) {
                toolbar.setTitle(viewPager.getCurrentItem());
            }*/
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (viewPager.getCurrentItem() == 0) {
                toolbar.setTitle("RECENT");
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new RecentFragment(), "RECENT");
    adapter.addFragment(new ContactFragment(), "CONTACT");
    adapter.addFragment(new Settingfragemnt(), "SETTING");
    viewPager.setAdapter(adapter);

}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {

        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

-1
    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <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/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMaxWidth="0dp"
            android:fillViewport="false" />

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

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



</android.support.design.widget.CoordinatorLayout>
Akash Shah
  • 15
  • 4
  • can u tell me the changes and explain it too? –  May 20 '17 at 05:34
  • set android:fitsSystemWindows="true" on both CoordinatorLayout & AppBarLayout and i am not including your child layout because I guess it was your ViewPager Page. viewPager's height is the same as the height for the entire root view. and Follow this link https://gist.github.com/iPaulPro/1468510f046cb10c51ea – Akash Shah May 20 '17 at 05:48
  • setting android:fitsSystemWindows="true" overlapping toolbar with titlebar –  May 20 '17 at 05:53
  • http://stackoverflow.com/questions/36557801/coordinatorlayout-appbarlayout-viewpager-not-resize-child-layout – Akash Shah May 20 '17 at 06:01
  • checked implemented but didn't work .... i just don't get that y everything is working fine recent fragment which codes same as setting_fragment –  May 20 '17 at 06:22
  • make one Common Fragment With One Edit Text of Xml and Replace Your Child With this Fragment – Akash Shah May 20 '17 at 06:36
  • and After Check that its Working or Not Can You Put Your Java Code? – Akash Shah May 20 '17 at 06:37
  • added java code and i added an textview in setting_fragment but not collapsing –  May 20 '17 at 06:42
  • I have Checked With Common Fragment With One Common Layout and Working Fine private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new Fragmenttwo(), "RECENT"); adapter.addFragment(new Fragmenttwo(), "CONTACT"); adapter.addFragment(new Fragmenttwo(), "SETTING"); viewPager.setAdapter(adapter); } So problem in Contact & Setting fragment – Akash Shah May 20 '17 at 07:21
  • then post onefragment class and xml in your ans not in commnet and as u can see above setting_fragment and there is nothing in Setting class so do u say there is something wrong with setting class –  May 20 '17 at 07:34
-1

MainActivity:

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    AppBarLayout appBarLayout;
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();
    }

    public void bindView() {
        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("RECENT");
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        TabLayout tabLayout;
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelected(true);
        viewPager.setCurrentItem(0);


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        toolbar.setTitle("RECENT");
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        toolbar.setTitle("CONTACT");
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        toolbar.setTitle("SETTING");
                        break;
                    default:
                        viewPager.setCurrentItem(0);
                        toolbar.setTitle("RECENT");
                        break;
                }
            /*if (viewPager.getCurrentItem() == 0) {
                toolbar.setTitle(viewPager.getCurrentItem());
            }*/
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (viewPager.getCurrentItem() == 0) {
                    toolbar.setTitle("RECENT");
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentOne(), "RECENT");
        adapter.addFragment(new Fragmenttwo(), "CONTACT");
        adapter.addFragment(new FragmentThree(), "SETTING");
        viewPager.setAdapter(adapter);

    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {

            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

activity_main:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="6dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"

            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:elevation="0dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="#FFF"
            app:tabMode="fixed"
            app:tabSelectedTextColor="#ffffff"
            app:tabTextColor="#FFF" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tab_layout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

Fragment One:

public class FragmentOne extends Fragment {

    public FragmentOne(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View oneFra = inflater.inflate(R.layout.fragment_one, container, false);


        return oneFra;
    }

}

fragment_one:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="nvojnvg"/>

</LinearLayout>

Fragmenttwo:

   public class Fragmenttwo extends Fragment {

    public Fragmenttwo(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View oneFra = inflater.inflate(R.layout.contact_fragment, container, false);


        return oneFra;
    }

}

contact_fragment xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- place your view here -->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_registered"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="2dp"
            android:background="#CCC" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_invite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:scrollbars="vertical" />
    </LinearLayout>


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

FragmentThree:

    public class FragmentThree extends Fragment {

    public FragmentThree(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View oneFra = inflater.inflate(R.layout.setting_fragment, container, false);


        return oneFra;
    }

}

setting_fragment xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</RelativeLayout>

Change Your Theme in Style:

 android:theme="@style/AppTheme.Base"

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:textColorPrimary">#1E1E1E</item>
        <item name="colorControlNormal">#1E1E1E</item>
        <item name="colorControlActivated">#BCBCBC</item>
        <item name="android:splitMotionEvents">false</item>
        <item name="android:windowEnableSplitTouch">false</item>
    </style>

See the image

Boken
  • 4,825
  • 10
  • 32
  • 42
Akash Shah
  • 15
  • 4
  • make sure that you have to import :import android.support.v4.app.Fragment; – Akash Shah May 20 '17 at 07:43
  • this is Working Fine in My Emulator Still Issue is Occure? – Akash Shah May 20 '17 at 07:44
  • can u tell me what problem r u checking at your side? –  May 20 '17 at 07:50
  • when You Swiped with Contact Fragment then Your app was crashed and coodinatorlayout+viewpager+TabLayout and have added three tabs with viewpager but scrolling only works with first tab – Akash Shah May 20 '17 at 07:55
  • not crashing at all i m facing the problem ....as mention in questions **i have added coodinatorlayout+viewpager+TabLayout and have added three tabs with viewpager but scrolling only works with first tab(recent) not working with two tabs 1. contact,2.setting** rewritten problem: collapsing ,scrolling not working in contact,setting fragment its only working in recent fragment –  May 20 '17 at 07:57
  • you have Collapsing Some Image With Toolbar( CollapsingToolbarLayout)? – Akash Shah May 20 '17 at 08:04
  • no there is no image collapsing with toolbar for now –  May 20 '17 at 08:16
  • scrolling not working in contact,setting fragment its only working in recent fragment that means whenever You Pressed On Recent Fragment and Slide to Top Up Then Your Action Bar is hiding But For remaining Two fragment that r not working? – Akash Shah May 20 '17 at 08:56
  • tell me actually What issue is occurred? – Akash Shah May 20 '17 at 09:17
  • can u post images of 1. with settitle to toolbar with not collapsed and 2. collapsed toolbar and the collapsed toolbar in setting fragment? –  May 20 '17 at 09:26
  • i have already post the code Check Your Self and i don't understand please tell me in brief – Akash Shah May 20 '17 at 09:38