D
D
Denis2016-08-19 01:30:06
Android
Denis, 2016-08-19 01:30:06

How to make ViewPager + TabLayout work in ScrollView?

Hello.
Faced a problem - ViewPager completely refuses to be displayed in ScrollView. At the same time, everything works as it should outside the ScrollView. The system does not give any errors.
In an attempt to solve this problem, I used solutions from these sites:
stackoverflow.com/questions/7381360/is-it-possible...
muratonnet.blogspot.ru/2014/01/android-viewpager-i...
and a few more related directly to the problem.
Here is the layout template for the fragment in which I display the ViewPager

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.strateg.test.NawigationDrawer"
    tools:showIn="@layout/app_bar_nawigation_drawer"
    android:id="@+id/main_layout">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <include layout="@layout/details_project_page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></include>

    </ScrollView>

</FrameLayout>

Here is the ViewPager + TabLayout (@layout/details_project_page) template
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layout_details_page_project">
    <android.support.design.widget.TabLayout
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tab_layout_details_project"
        app:tabIndicatorColor = "@color/color_blue_text"
        app:tabIndicatorHeight="6dp"
        app:tabSelectedTextColor="@color/color_black_head_project_page"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
         />

    <android.support.v4.view.ViewPager

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content_details_tabs_layout"
        app:layout_behavior = "@string/appbar_scrolling_view_behavior"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/tab_layout_details_project"
        android:fillViewport="true"
        android:forceHasOverlappingRendering="false"
        android:nestedScrollingEnabled="false"
        android:touchscreenBlocksFocus="false"
        android:transitionGroup="false">

    </android.support.v4.view.ViewPager>

</RelativeLayout>

Here is the class that manages ViewPager + TabLayout
public class DetailsTabLayoutPageProject {

    private ViewPager content_details_tabs_layout;

    private void init_tabs()
    {

    }

    DetailsTabLayoutPageProject(FragmentManager fragmentManager, View parent)
    {
        content_details_tabs_layout = (ViewPager) parent.findViewById(R.id.content_details_tabs_layout);
        AdapterFragmentTabs adapter = new AdapterFragmentTabs(fragmentManager);
        content_details_tabs_layout.setAdapter(adapter);
        TabLayout tab_layout_details_project = (TabLayout) parent.findViewById(R.id.tab_layout_details_project);
        tab_layout_details_project.setupWithViewPager(content_details_tabs_layout );
    }

    class AdapterFragmentTabs extends FragmentPagerAdapter {
        private String tabs[];

        public AdapterFragmentTabs(FragmentManager fm) {
            super(fm);
            tabs = new String[]{
                    "1 вклвдка",
                    "2 вклвдка",
                    "3 вклвдка",
                    "4 вклвдка",
                    "5 вклвдка",
            };
        }

        @Override
        public Fragment getItem(int position) {
            switch (position)
            {
                default:return MyExampleFragment.getInstance();
            }
        }

        @Override
        public CharSequence getPageTitle(int position) {
            //return super.getPageTitle(position);
            return  tabs[position];
        }

        @Override
        public int getCount() {
            return tabs.length;
            //return 0;
        }
    }

    static class MyExampleFragment extends Fragment{
        private static final int LAYOUT = R.layout.description_project_page_fragment;
        private View view;

        public static MyExampleFragment getInstance()
        {
            Bundle args = new Bundle();
            MyExampleFragment fragment = new MyExampleFragment();
            fragment.setArguments(args);
            return fragment;
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(LAYOUT, container, false);
            return view;
        }
    }
}

Please tell me how to make ViewPager to be displayed in ScrollView.
Or maybe you need to use another container instead of ScrollView.
Or suggest how you can replace the ViewPager + TabLayout bundle, but with the same functionality.
Or if you have a working code in which the ViewPager is displayed in a ScrollView, then please give a link to it, maybe I can find the reason by comparing the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-08-23
@lacredin

The ViewPager must have a fixed height when in a ScrollView. Accordingly, if you need a dynamic height, you will have to change it from the java code.
In my case, since the ViewPager is located in a different layout file, it is necessary to set the static height not for the ViewPager, but through which it is connected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question