2

我在 ViewPager 中使用 RecyclerView,在 CoordinatorLayout 中使用整个 Viewpager。

我想用滚动 RecyclerView 滚动 ImageLayout。

请看这张图片。我想在滚动 recyclerView 时滚动这个顶部横幅

以下是我的代码,请帮助我。

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>

片段主页.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

recyclerView.setNestedScrollingEnabled(false);
4

1 回答 1

3

首先,您需要覆盖您的 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);
     }

也为 RecyclerView 添加recyclerView.setHasFixedSize(true)

于 2017-05-05T07:08:20.750 回答