Hanzyusuf
Hanzyusuf

Reputation: 379

NestedScrollView stops scrolling as soon as CollapsingToolbarLayout collapses

First let me clarify that i have found a few questions similar to this question, but none worked for me.

So i have one activity (MainActivity.java), which has a bottom navigation tab, each tab has its own fragment, the third fragment is named 'ServiceFragment.java' (also the third tab in Bottom navigation).

Classes:

ServiceFragment.java

package in.ikleen.ikleenservices;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ServiceFragment extends Fragment {

    Context context;

    public ServiceFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach (Context context){
        super.onAttach(context);
        this.context = context;
    }


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

        ViewPager viewPager = rootView.findViewById(R.id.service_view_pager);
        viewPager.setOffscreenPageLimit(3);
        TabLayout tabLayout = rootView.findViewById(R.id.service_tab_layout);
        ServiceFragmentPageAdapter pageAdapter = new ServiceFragmentPageAdapter(context, getChildFragmentManager());
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);

        return rootView;
    }

}

ServiceFragmentPageAdapter.java

package in.ikleen.ikleenservices;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ServiceFragmentPageAdapter extends FragmentPagerAdapter {
    private Context mContext;
    Fragment zero, first, second;

    public ServiceFragmentPageAdapter (Context context, FragmentManager fm){
        super(fm);
        mContext = context;
        zero = new WashDryServiceFragment();
        first = new WashDryIronServiceFragment();
        second = new AdditionalProductsServiceFragment();
    }

    @Override
    public Fragment getItem(int position){
        switch (position){
            case 0:
                return zero;
            case 1:
                return first;
            case 2:
                return second;
            default:
                return zero;
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position){
        switch (position){
            case 0:
                return mContext.getString(R.string.wash_dry);
            case 1:
                return mContext.getString(R.string.wash_dry_iron);
            case 2:
                return mContext.getString(R.string.additional_products);
            default:
                return mContext.getString(R.string.wash_dry);
        }
    }
}

WashDryServiceFragment.java (the first tab of viewpager tablayout, also the one bearing the problem)

package in.ikleen.ikleenservices;


import android.annotation.TargetApi;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toolbar;

import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;

public class WashDryServiceFragment extends Fragment {


    public WashDryServiceFragment() {
        // Required empty public constructor
    }


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

        ListView listView = (ListView) rootView.findViewById(R.id.listViewWashDryService);

        ArrayList<String> stringList = new ArrayList<>();
        for(int i=0; i<20; i++) {
            stringList.add("Hello");
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, stringList);
        listView.setAdapter(adapter);

        //TESTED BELOW CODE BUT CANNOT EVEN SCROLL A LITTLE BIT SO COMMENTED IT
        //Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar1);
        //NestedScrollView nestedScrollView = (NestedScrollView) rootView.findViewById(R.id.nestedScroll);
        //CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) nestedScrollView.getLayoutParams();
        //params.setBehavior(new ConstrainedScrollBehavior());

        return rootView;
    }

}

Layouts:

fragment_service.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="in.ikleen.ikleenservices.ServiceFragment">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/service_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/Base.TextAppearance.AppCompat.Small"
            app:tabMode="fixed"/>

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

    </LinearLayout>

</FrameLayout>

fragment_wash_dry_service.xml (THE PROBLEM LIES HERE)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="in.ikleen.ikleenservices.WashDryServiceFragment">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorlayout"
        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="150dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    app:layout_collapseMode="parallax"
                    android:scaleType="centerInside"
                    app:layout_collapseParallaxMultiplier="0.5"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    android:src="@mipmap/ld_00"/>

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

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScroll"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_gravity="fill_vertical">

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

                <ListView
                    android:id="@+id/listViewWashDryService"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/appBarLayout"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="JUST FOR TESTS"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

            </LinearLayout>

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

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

</FrameLayout>

Thanks

Upvotes: 0

Views: 667

Answers (1)

Hanzyusuf
Hanzyusuf

Reputation: 379

The problem in this case was: (p.s. i am stupid)

listview cannot scroll inside nestedscrollview, so as soon as the collapsingtoolbar collapsed, there was no need for the nestedscrollview to scroll more as it already filled the parent and listview could not scroll anyways. (i know, dumb explanation)

Solution:

1: either remove listview inside the nestedscrollview

OR

2: add this attribute android:nestedScrollingEnabled="true" inside nestedscrollview (not tested, but should work)

Upvotes: 1

Related Questions