【问题标题】:Content with multiple RecyclerView and a ViewPager not scrolling inside NestedScrollView具有多个 RecyclerView 和 ViewPager 的内容不在 NestedScrollView 内滚动
【发布时间】:2016-12-05 08:25:29
【问题描述】:

我遇到了一个奇怪的问题。我有一个带有两个 RecyclerView 的 xml 和一个带有 NestedScrollview 内的圆形页面指示器的 ViewPager。我使用 layout_weight 和 weightSum 在屏幕上显示小部件。 RecyclerView 之一具有水平布局,可以很好地水平滚动。我想实现单垂直滚动,但不幸的是它不起作用。

我在 xml 中使用了“app:layout_behavior="@string/appbar_scrolling_view_behavior”,在 java 代码中使用了“recyclerview.setNestedScrollingEnabled(false)”。

这是我的 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>

这是我的 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"/>

【问题讨论】:

    标签: android android-viewpager android-recyclerview viewpagerindicator nestedscrollview


    【解决方案1】:

    似乎每个人都在其他地方忙 :) 无论如何,我设法自己找到了解决方案。

    导致问题的潜在问题有三个:

    1. 在父布局中使用“weightSum”和
    2. 布局中的 ViewPager
    3. ViewPager 默认采用全屏。

    我使用“weightSum”来控制 ViewPager 的大小并显示其他两个 Recyclerview。为了删除“weightSum”,我自定义了 ViewPager,这样它就只需要它最大的孩子的高度。下面是自定义的 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();
    }
    }
    

    第二步,我从 RecyclerView 中删除了“layout_weight”,并将其“layout_height”设置为“wrap_content”并完成了工作。

    以下是更新后的布局代码:

    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"/>
    

    就是这样!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多