Saef Myth
Saef Myth

Reputation: 297

RecyclerView inside view-pager inside a scrollview

i have this layout >

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fillViewport="true">

        <LinearLayout
            android:id="header_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
             >
            <android.support.v7.widget.RecyclerView
                android:id="@+id/moods_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:scrollbars="none" />

            <android.support.v4.view.ViewPager
                android:id="@+id/mainwindow_view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

    </ScrollView>


    <android.support.design.widget.TabLayout
        android:id="@+id/mainwindow_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/view_border_top" />
</LinearLayout>

An d ViewPager Fragment have this code

<?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="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/net_rclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:layout_weight="0"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>
</LinearLayout>

i want the

header_content layout

to be scrolled out when the

net_rclerview inside the view-pager fragment

is scrolled. the problem is recyclerview is scrolling but the above content is not scrolling out of the view ,, is this possible to achieve and if there are any other options i can do ?

Upvotes: 1

Views: 3105

Answers (2)

Saira Nawaz
Saira Nawaz

Reputation: 720

I just came across similar problem and after a lot of surfing I figured out that issue was not with RecyclerView but with ViewPager. ViewPager 's WrapContent property does not work when put inside ScrollView. So a fixed height restrict RecyclerView to showUp full contents. Just Update ViewPager's OnMeasure Method as Follow:

public class CustomViewPager extends ViewPager {

    boolean canSwipe = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View child = getChildAt(getCurrentItem());
        if (child != null) {
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void canSwipe(boolean canSwipe) {
        this.canSwipe = canSwipe;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.canSwipe) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.canSwipe) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }
}

It worked for me. Hope it works for you as well.

Upvotes: 3

AskNilesh
AskNilesh

Reputation: 69681

use NestedScrollView instead of ScrollView

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

and

make RecyclerView.setNestedScrollingEnabled(false); to your recyclerview

Upvotes: 0

Related Questions