[英]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. 我在NestedScrollview中有一个带有两个RecyclerView和一个带有圆形页面指示器的ViewPager的xml。 I have used layout_weight and weightSum to show widget on screen. 我已经使用layout_weight和weightSum在屏幕上显示小部件。 One of RecyclerView has horizontal layout which is scrolling horizontally fine. RecyclerView之一具有水平布局,可以水平滚动。 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. 我在xml中使用了“ app:layout_behavior =“ @ string / appbar_scrolling_view_behavior”,在Java代码中使用了“ recyclerview.setNestedScrollingEnabled(false)”。
here is my fragment_home.xml 这是我的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 这是我的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: 存在导致问题的三个潜在问题:
I have used "weightSum" to control size of ViewPager and showing other two Recyclerview. 我已经使用“ weightSum”来控制ViewPager的大小并显示其他两个Recyclerview。 To remove "weightSum" I customised ViewPager so that it only takes height of its biggest children. 为了删除“ weightSum”,我对ViewPager进行了自定义,使其仅占用其最大子级的高度。 Below are the customised ViewPager code: 以下是自定义的ViewPager代码:
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. 第二步,我从两个RecyclerView中都删除了“ layout_weight”,并将其“ layout_height”设置为“ wrap_content”,并完成了工作。
Below are the updated layout code: 以下是更新的布局代码:
fragment_home.xml 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 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!!! 而已!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.