简体   繁体   English

NestedScrollView无法与ViewPager内的RecyclerView一起使用

[英]NestedScrollView was not working with RecyclerView which is inside of ViewPager

I am using RecyclerView inside the ViewPager and whole Viewpager inside the CoordinatorLayout. 我在ViewPager内部使用RecyclerView,在CoordinatorLayout内部使用整个Viewpager。

I want to Scroll ImageLayout with Scrolling RecyclerView. 我想使用滚动RecyclerView滚动ImageLayout。

Please See this Image. 请看这张图片。 我想在滚动recyclerView时滚动此顶部横幅

Following are my code so please help me. 以下是我的代码,请帮助我。

home_activty.xml home_activty.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">


<!-- The main content view where fragments are loaded -->
<FrameLayout
    android:id="@+id/flContent2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="fill"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ToolbarColoredBackArrow"
            app:titleTextAppearance="@style/TabLayoutTextStyle" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="3dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="#d8d8d8" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/rlbelow"

        android:background="#f0f0f0"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.v4.widget.NestedScrollView>

    <RelativeLayout
        android:id="@+id/rlbelow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="bottom">

        <LinearLayout
            android:id="@+id/llplaceorder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/input_bar"
            android:background="@color/colorPrimary"
            android:orientation="horizontal"
            android:visibility="gone">

            <TextView
                android:id="@+id/txtplace"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:textAlignment="center"
                android:textColor="@color/colorappname"
                android:textSize="17dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/input_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/input_summary"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="58dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="10dp"
                android:gravity="right|top"
                android:orientation="vertical"
                android:padding="3dp"
                android:visibility="visible">

                <ImageView
                    android:id="@+id/imageCart"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/cart_outlinebk"
                    android:gravity="center_vertical|center_horizontal" />

                <TextView
                    android:id="@+id/txtcardcount"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:background="@drawable/xml_budget_green"
                    android:padding="4dp"
                    android:text="99"
                    android:textAlignment="center"
                    android:textColor="#ffffff"
                    android:textSize="10dp" />
            </RelativeLayout>

            <TextView
                android:id="@+id/txvlin"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/xml_vline_green"
                android:visibility="visible" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/home_total_itmes"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@color/PrimaryText"
                        android:textSize="14dp" />

                    <TextView
                        android:id="@+id/textTotalQty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@color/PrimaryText"
                        android:textSize="14dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/home_totalrs"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@color/PrimaryText"
                        android:textSize="14dp" />

                    <TextView
                        android:id="@+id/textviewTotal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@color/PrimaryText"
                        android:textSize="14dp"
                        android:textStyle="bold" />
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:gravity="center_vertical|center_horizontal"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/imageNext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/arrow_right_xmlbk" />
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>


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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home"
    app:menu="@menu/activity_home_drawer" />

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

fragment_home.xml fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0">

    <include
        android:id="@+id/banner"
        layout="@layout/row_topimage" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/banner"
        android:layout_toEndOf="@+id/relativeLayout"
        android:layout_toRightOf="@+id/relativeLayout">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f0f0f0"
            android:paddingBottom="80dp"
            android:visibility="visible"/>


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

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/EMPTYTEXT"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily=""
        android:gravity="center"
        android:paddingBottom="80dp"
        android:text="Recenty No Products available"
        android:textColor="#000"
        android:textStyle="bold"
        android:visibility="gone" />
</RelativeLayout>

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone" />


    </RelativeLayout>

homeFragment.java homeFragment.java

recyclerView.setNestedScrollingEnabled(false);

First you need to override your Viewpager and add 首先,您需要覆盖Viewpager并添加

 @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, 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);
     }

Also for RecyclerView add recyclerView.setHasFixedSize(true) ; 也为RecyclerView添加recyclerView.setHasFixedSize(true) ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在包含在ViewPager下面的RecyclerView的NestedScrollView内部未显示ViewPager - ViewPager not showing inside NestedScrollView which contains RecyclerView below ViewPager NestedScrollView内部的RecyclerView无法正常工作 - RecyclerView inside of NestedScrollView not working 带有多个RecyclerView和一个ViewPager的内容不在NestedScrollView中滚动 - Content with multiple RecyclerView and a ViewPager not scrolling inside NestedScrollView RecyclerView 滚动在 NestedScrollView 内不起作用 - RecyclerView Scroll Not Working Inside NestedScrollView NestedScrollView 中的 ViewPager 中的 RecyclerView - 效果不佳 - RecyclerView inside ViewPager inside NestedScrollView - doesn't work well nestedscrollview滚动问题内的片段中的Recyclerview - Recyclerview in fragment which is inside nestedscrollview scrolling issue 如何在2.6内部嵌套的RecyclerView中实现分页 - How to implement pagination in RecyclerView which is inside nestedScrollView 如何在NestedScrollView中使用RecyclerView创建ViewPager - How can I make work ViewPager with RecyclerView inside NestedScrollView recyclerview 中的最后一项(在nestedscrollview 内)被剪切,横向剪切viewpager - Last item in recyclerview (inside a nestedscrollview) is cut, landscape orientation cuts the viewpager Nestedscrollview 内的 Viewpager2 包含两个 Fragment,Recyclerview 有问题 - Viewpager2 inside Nestedscrollview consist two Fragment with Recyclerview having problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM