Android enthusiast
Android enthusiast

Reputation: 21

View Pager is not working inside nested scroll view in android

Hello people I'm a newbie to android development and I'm stuck at this point where my view pager is not working as expected. What I want to achieve is a tab layout scrolling up along with a collapsing toolbar layout. Thanks a lot :) Without the nested scroll view the problem is that that collapsing toolbar is not collapsing.This is the code :

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@android:color/black"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/bottom_nav_home_icon"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.1" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.design.widget.TabLayout
            android:id="@+id/temp_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:theme="@style/TabLayoutTheme" />
        <android.support.v4.view.ViewPager
            android:id="@+id/temp_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


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

This is working well for view pager but the collapsing toolbar is not collapsing.When I tried to keep it inside a nested scroll view the collapsing toolbar layout is collapsing but view pager is not working. It is not sliding the tabs.

The java code for view pager is

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;



public class Tabs extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_history_tabs);
        tabLayout=(TabLayout)findViewById(R.id.temp_tab);
        viewPager=(ViewPager)findViewById(R.id.temp_view_pager);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
    }
    class PageAdapter extends FragmentPagerAdapter{

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new TabFragment1();
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:return "A";
                default:return "B";
            }
        }
    }
}

Upvotes: 2

Views: 1075

Answers (2)

hchhabra
hchhabra

Reputation: 71

Try putting your LinearLayout inside a NestedScrollView,

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/nsv">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/temp_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    </LinearLayout>

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

and setting android:fillViewport to true for the NestedScrollView.

NestedScrollView nestedScrollView = (NestedScrollView) findViewById (R.id.nsv);
nestedScrollView.setFillViewport (true);

Upvotes: 1

Dharmishtha
Dharmishtha

Reputation: 1343

i think

android:fitsSystemWindows="true" is the cause. Remove this line.May solve your issue.

Upvotes: 0

Related Questions