简体   繁体   中英

Content with multiple RecyclerView and a ViewPager not scrolling inside NestedScrollView

I'm facing a strange problem. I have a xml with two RecyclerView and a ViewPager with circle page indicator inside a NestedScrollview. I have used layout_weight and weightSum to show widget on screen. One of RecyclerView has horizontal layout which is scrolling horizontally fine. I want to achieve single vertical scrolling but unfortunately it is not working.

I have used "app:layout_behavior="@string/appbar_scrolling_view_behavior" in xml and "recyclerview.setNestedScrollingEnabled(false)" in java code.

here is my fragment_home.xml

<android.support.v4.widget.NestedScrollView 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/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.e2e.qnamo.fragment.HomeFragment">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10"
    android:descendantFocusability="blocksDescendants">

    <include
        layout="@layout/layout_pager_indicator"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_hot_topic"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="2"
        tools:listitem="@layout/hot_topic_row" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_category"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="5"
        tools:listitem="@layout/category_row"/>


</LinearLayout>

here is my layout_pager_indicator.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="wrap_content"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/bannerViewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/viewpager_indicator_single_item" />

<com.e2e.qnamo.widget.CirclePageIndicator
    android:id="@+id/pager_indicator"
    style="@style/CustomCirclePageIndicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_gravity="center|bottom"/>

Seems like every one was busy else where :) Anyway I managed to find solution by myself.

There were three potential problem which causing problem:

  1. Using "weightSum" in parent layout and
  2. ViewPager in layout
  3. ViewPager takes whole screen by default.

I have used "weightSum" to control size of ViewPager and showing other two Recyclerview. To remove "weightSum" I customised ViewPager so that it only takes height of its biggest children. Below are the customised ViewPager code:

public class CustomViewPager extends ViewPager
{
    private int mCurrentPagePosition = 0;

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    for(int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if(h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void reMeasureCurrentPage(int position) {
    mCurrentPagePosition = position;
    requestLayout();
}
}

Second step I did that I removed "layout_weight" from both of RecyclerView and set it "layout_height" to "wrap_content" and work done.

Below are the updated layout code:

fragment_home.xml

<android.support.v4.widget.NestedScrollView 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/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:fillViewport="true"
tools:context="com.e2e.qnamo.fragment.HomeFragment">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/main_container_margin"
    android:layout_marginTop="@dimen/main_container_margin"
    android:layout_marginRight="@dimen/main_container_margin"
    android:layout_marginBottom="@dimen/main_container_margin"
    android:orientation="vertical">

    <include
        layout="@layout/layout_pager_indicator"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_hot_topic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/recycler_margin_top"
        tools:listitem="@layout/hot_topic_row" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_category"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/recycler_margin_top"
        android:layout_marginBottom="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/category_row"/>


</LinearLayout>

layout_pager_indicator.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="wrap_content"
android:orientation="vertical">

<com.e2e.qnamo.widget.CustomViewPager
    android:id="@+id/bannerViewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/viewpager_indicator_single_item" />

<com.e2e.qnamo.widget.CirclePageIndicator
    android:id="@+id/pager_indicator"
    style="@style/CustomCirclePageIndicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_gravity="center|bottom"/>

Thats it!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM