如何在nestedScrollView中使用viewpager和viewpager的视图顶部 [英] How to use viewpager inside nestedScrollView with a view top of viewpager

查看:103
本文介绍了如何在nestedScrollView中使用viewpager和viewpager的视图顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于viewpager,我无法滚动nestedScrollView.因此,在这里让我解释一下我要创建的布局设计:

I am unable to scroll nestedScrollView because of viewpager. So let me explain here is layout design that i want to create:

NestedScrollView中有FrameLayout,还有一个带有选项卡布局的viewpager. Viewpager用无休止的recylerview(分页)加载三个片段.

There is FrameLayout inside NestedScrollView and a viewpager with tab layout. Viewpager load three fragments with endless recylerview (Pagination).

这是布局xml代码:

<android.support.design.widget.CoordinatorLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context="com.plowns.droidapp.activites.HomeActivity">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </RelativeLayout>


    </android.support.v7.widget.Toolbar>

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="50dp"
    android:background="@color/colorPrimary" />


<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedscrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="@color/trans">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="260dp">

                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="55dp"
                    app:cardCornerRadius="5dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <ImageView
                            android:id="@+id/txt_edit_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:src="@drawable/ic_mode_edit"
                            android:tint="@color/blue_plowns" />

                        <TextView
                            android:id="@+id/txt_child_name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="50dp"
                            android:ellipsize="end"
                            android:gravity="center_vertical"
                            android:minHeight="5dp"
                            android:text="Satwinder Singh"
                            android:textAppearance="?android:attr/textAppearanceListItemSmall"
                            android:textColor="@android:color/black"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                        <LinearLayout
                            android:id="@+id/ll_points_class"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/txt_child_name"
                            android:layout_centerHorizontal="true"
                            android:orientation="horizontal">

                            <TextView
                                android:id="@+id/txt_child_points"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:text="10038"
                                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                android:textColor="@color/gray"
                                android:textSize="14sp" />

                            <View
                                android:id="@+id/point_class_sapretor"
                                android:layout_width="1dp"
                                android:layout_height="10dp"
                                android:layout_gravity="center"
                                android:layout_marginLeft="5dp"
                                android:background="@color/gray" />

                            <TextView
                                android:id="@+id/txt_child_class"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="5dp"
                                android:text="Collage"
                                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                android:textColor="@color/gray"
                                android:textSize="13sp" />

                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/ll_title"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/ll_points_class"
                            android:layout_centerHorizontal="true"
                            android:orientation="horizontal"
                            android:visibility="gone">

                            <TextView
                                android:id="@+id/txt_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:ellipsize="end"
                                android:maxLines="1"
                                android:singleLine="true"
                                android:text=""
                                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                android:textColor="@android:color/white"
                                android:textSize="14sp" />

                        </LinearLayout>


                        <android.support.v7.widget.CardView
                            android:id="@+id/card_badge"
                            android:layout_width="100dp"
                            android:layout_height="20dp"
                            android:layout_below="@+id/ll_title"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:backgroundTint="@color/yellow_plowns"
                            app:cardCornerRadius="5dp">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:text="BADGE WALL"
                                android:textColor="@color/white"
                                android:textSize="12sp"
                                android:textStyle="bold" />

                        </android.support.v7.widget.CardView>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_below="@+id/card_badge"
                            android:layout_centerHorizontal="true"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:layout_marginTop="5dp"
                            android:baselineAligned="false"
                            android:gravity="center"
                            android:orientation="horizontal"
                            android:weightSum="3">

                            <RelativeLayout
                                android:id="@+id/rl_upload"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:paddingTop="5dp">

                                <TextView
                                    android:id="@+id/txt_upload_count"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_centerHorizontal="true"
                                    android:text="13"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/black"
                                    android:textSize="15sp"
                                    android:textStyle="bold" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/txt_upload_count"
                                    android:layout_centerHorizontal="true"
                                    android:layout_marginTop="5dp"
                                    android:text="@string/profile_title_uploads"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/gray"
                                    android:textSize="13sp" />

                                <View
                                    android:layout_width="1dp"
                                    android:layout_height="40dp"
                                    android:layout_alignParentRight="true"
                                    android:layout_centerVertical="true"
                                    android:background="@color/gray" />


                            </RelativeLayout>

                            <RelativeLayout
                                android:id="@+id/rl_followers"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:paddingTop="5dp">

                                <TextView
                                    android:id="@+id/txt_follower_count"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_centerHorizontal="true"
                                    android:text="13"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/black"
                                    android:textSize="15sp"
                                    android:textStyle="bold" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/txt_follower_count"
                                    android:layout_centerHorizontal="true"
                                    android:layout_marginTop="5dp"
                                    android:text="@string/profile_title_followers"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/gray"
                                    android:textSize="13sp" />

                                <View
                                    android:layout_width="1dp"
                                    android:layout_height="40dp"
                                    android:layout_alignParentRight="true"
                                    android:layout_centerVertical="true"
                                    android:background="@color/gray" />

                            </RelativeLayout>

                            <LinearLayout
                                android:id="@+id/ll_following"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/txt_following_count"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="47"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/black"
                                    android:textSize="15sp"
                                    android:textStyle="bold" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginTop="5dp"
                                    android:text="@string/profile_title_following"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@color/gray"
                                    android:textSize="13sp" />


                            </LinearLayout>

                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="25dp"
                            android:layout_alignParentBottom="true"
                            android:background="@color/colorPrimary"
                            android:orientation="horizontal">

                            <LinearLayout
                                android:id="@+id/ll_share"
                                android:layout_width="0dp"
                                android:layout_height="match_parent"
                                android:layout_weight="0.5"
                                android:gravity="center"
                                android:orientation="horizontal">

                                <TextView
                                    android:id="@+id/txt_share_icon"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:gravity="center"
                                    android:text="&#xf0c0;"
                                    android:textColor="@color/pink"
                                    android:textSize="15sp" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="10dp"
                                    android:gravity="center"
                                    android:text="Invite friends"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@android:color/white"
                                    android:textSize="14sp" />

                            </LinearLayout>

                            <LinearLayout
                                android:id="@+id/ll_switch_child"
                                android:layout_width="0dp"
                                android:layout_height="match_parent"
                                android:layout_weight="0.5"
                                android:gravity="center"
                                android:orientation="horizontal">

                                <ImageView
                                    android:id="@+id/txt_switch_child_icon"
                                    android:layout_width="22dp"
                                    android:layout_height="22dp"
                                    android:background="@drawable/ic_switch"
                                    android:textAllCaps="false"
                                    android:textColor="@color/white" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="10dp"
                                    android:gravity="center"
                                    android:text="Switch profile"
                                    android:textAppearance="?android:attr/textAppearanceListItemSmall"
                                    android:textColor="@android:color/white"
                                    android:textSize="14sp" />

                            </LinearLayout>

                        </LinearLayout>

                    </RelativeLayout>

                </android.support.v7.widget.CardView>

            </FrameLayout>

            <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/img_child_profile"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="top|center_horizontal"
                android:layout_marginTop="5dp"
                android:src="@drawable/blank_profile"
                app:border_color="@color/colorPrimary"
                app:border_width="2dp" />

        </FrameLayout>



        <android.support.v7.widget.CardView
            android:id="@+id/card_bottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp"
            app:cardCornerRadius="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    app:tabGravity="fill"
                    app:tabIndicatorColor="@color/colorPrimary"
                    app:tabIndicatorHeight="0dp"
                    app:tabMode="fixed"
                    app:tabSelectedTextColor="@color/white"
                    app:tabTextColor="@color/white" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

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

现在,当我在viewpager之外滚动时,什么也没发生(视图未滚动).并且在viewpager上滚动仅recyclerview滚动.但是我想在外部框架布局滚动和recylerview上滚动整个视图.

Now when i scroll outside of viewpager nothing happened (view not scroll). And on viewpager scroll only recyclerview scroll. But I want to scroll the whole view on outside frame layout scroll and on recylerview also.

我尝试过的方法:

1)为Viewpager设置固定高度(700dp). 结果:在Framelayout上滚动可正常工作,但在Recyclerview上无法正常滚动.我必须滚动很多次才能滚动.

1) Set fixed height for Viewpager(700dp). Result: Scroll on Framelayout smoothly worked but Scroll on Recyclerview is not worked correctly and smooth. I have to scroll many time to scroll.

2)设置框架布局和AppBarLayout内的Tablayout. 结果:向上滚动工作正常,但在向下滚动Recylerview(Viewpager)时,首先完全向下滚动了Appbarlayout,然后滚动了位于Appbarlayout后面的Recyclerview内容.因此,我必须向下滚动整个列表才能看到第一个内容,这是一种糟糕的体验.

2) Set Framelayout & Tablayout inside AppBarLayout. Result: Scroll Up worked smoothly but On Scroll down Recylerview(Viewpager), first Appbarlayout is fully scrolled down and then Recyclerview content scrolled that was behind Appbarlayout. So I have to scroll down the whole list to see first contents it is a bad experience.

3.),按照此处的建议创建自定义Viewpager,例如在此处输入链接描述 结果:与第一种情况相同:

3.) Created custom Viewpager as suggested here like enter link description here Result : Same as 1st case:

4)已禁用触摸Viewpager并使用自定义Viewpager. 结果:整个视图平滑滚动,但是由于Viewpager上的触摸被禁用,我无法对Recyclerview进行分页,这意味着必须检查每种触摸类型并检测滚动的结尾才能在Recyclerview中加载下一个数据.我不想这样做.

4) Disabled Touch on Viewpager and use custom Viewpager. Result: Smoothly scrolled whole view but due to touch disabled on Viewpager I am unable to paginate Recyclerview means have to check every touch type and detect the end of the scroll to load next data in Recyclerview. I do not want to do this.

这是我的问题.如果有人有解决方案或类似的东西,请提供帮助.提前致谢.

So here is my problem. Please if anybody has solution or cretaed somthing like this please help. Thanks in advance.

推荐答案

因此,我终于通过仅创建Custom Viewpager来解决此问题,如下所述:

So I have finally resolved this problem by just creating Custom Viewpager as mentioned here: Custom ViewPager that calculate ViewPager height and setNestedScrollingEnable(false) to RecyclerView inside Viewpager. By setting NestedScrollingEnalbe = false Recyclerview does set parent NestedScrollView's scroll. So Parent NestedScrollView works with inside Viewpager.

这篇关于如何在nestedScrollView中使用viewpager和viewpager的视图顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆