本篇效果图:

新增成果(不同于本篇成果的另一种成果,蕴含在本我的项目中):

第一张图 通过RecyclerView+Vlayout多布局实现;第二张具备实战性质的效果图 通过CoordinatorLayout+RecyclerView实现;

第一版得布局结构图:

起初思考到TabLayoutRecyclerView(ViewPager中)能够一起滑动,所以很容易想到的方法就是用Scrollview将两者嵌套进去,成果是实现了,然而Scrollview嵌套Viewpager的弊病不言而喻!

而第二版即本篇博客并不是为了解决Scrollview嵌套Viewpager的问题,而是换一种思路去实现!

布局结构图,很简略,就两层:

 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#f7f7f7"    android:focusable="true"    android:focusableInTouchMode="true">    <LinearLayout        android:id="@+id/ll_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <net.lucode.hackware.magicindicator.MagicIndicator            android:id="@+id/magicIndicator"            android:layout_width="match_parent"            android:layout_height="35dp"            android:background="#acddee"            android:visibility="gone" />        <com.byl.jdrefresh.v1.CustomViewPager            android:id="@+id/customViewPager"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </LinearLayout>    <RelativeLayout 搜寻栏.../></RelativeLayout>

就是将第一版中的第一层和第二层(自定义JdScrollVIew)放在了Tab1的fragment中:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.byl.jdrefresh.v2.JdScrollView2        android:id="@+id/jdScrollView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

JdScrollView布局 仅须要将原来布局中的ViewPager换成RecyclerView即可,具体可参考源码!

但这样做如同并没有解决TabLayout和列表一起滑动的成果啊?!

其实,这里取了一个巧,MainActivity中的有一个TabLayout,而tab1也就是首页中的Fragment也蕴含了一个一摸一样的TabLayoutNestedScrollview嵌套TabLayout+RecyclerView),当viewpager的position==0时,MainActivity中的TabLayout暗藏,其它页面时显示,所有的成果操作由MainActivity转移到了Tab1Fragment中,这样也就防止了应用ScrollView嵌套Viewpager这种模式!

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/rl_content"    android:layout_width="match_parent"    android:layout_height="match_parent">    <View        android:id="@+id/view"        android:layout_width="match_parent"        android:layout_height="180dp"        android:background="#acddee" />    <ImageView        android:id="@+id/iv_ad"        android:layout_width="match_parent"        android:layout_height="1000dp"        android:layout_marginTop="-820dp"        android:scaleType="centerCrop"        android:src="@mipmap/bg_ad" />    <LinearLayout        android:id="@+id/ll_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <TextView            android:id="@+id/tv_refresh_state"            android:layout_width="match_parent"            android:layout_height="40dp"            android:gravity="center"            android:text="下拉刷新"            android:textColor="#dddddd" />        <net.lucode.hackware.magicindicator.MagicIndicator            android:id="@+id/magicIndicator"            android:layout_width="match_parent"            android:layout_height="35dp" />        <com.byl.jdrefresh.v2.CustomRecyclerView            android:id="@+id/recyclerView"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginHorizontal="15dp"            android:nestedScrollingEnabled="false" />    </LinearLayout></RelativeLayout>

另外,本篇在原来的根底上多加了一个性能,能够参考京东app,即下拉超过肯定间隔后,背景会主动向下全屏开展,而后主动进入到广告页面:

实现计划,就是在手势抬起(ACTION_UP)时,判断以后下拉的间隔,超过某一设定值时,则主动在肯定工夫内让图片及整体布局处于全屏状态,其实就是依附ValueAnimator,一直的设置背景图的marginTop以及内容的paddingTop

case MotionEvent.ACTION_UP:                if (adScrollDistance > 500) {                    isInterceptTouch = true;                    AnimUtils.start(-(int) (marginTop + adScrollDistance), 500, new AnimUtils.OnAnimListener() {                        @Override                        public void onUpdate(int value) {                            layoutAd(marginTop + adScrollDistance + value);                            ll_content.setPadding(0, (int) (paddingTop + adScrollDistance + AD_START_SCROLL_DISTANCE) + value, 0, 0);                        }                        @Override                        public void onEnd() {                            context.startActivity(new Intent(context, AdActivity.class));                            new Handler().postDelayed(() -> {                                tv_refresh_state.setText("下拉刷新");                                isInterceptTouch = false;                                recyclerView.setRefreshing(false);                                isInterceptScroll = false;                                REFRESH_STATUS = REFRESH_DONE;                                layoutAd(marginTop);                                iv_ad.setImageAlpha(0);                                if (onPullListener != null) onPullListener.onPull(255);                                ll_content.setPadding(0, paddingTop, 0, 0);                                reset();                            }, 300);                        }                    });                    return true;                }                ......

有一点须要留神的是,背景图片的高度,并不是屏幕高度,而是屏幕的高度加上

这一部分的高度:

screenHeight = SysUtils.getScreenHeight(context);topRemainHeight = SysUtils.Dp2Px(context, imageShowHeight) - StatusBarUtil.getStatusBarHeight(context) - SysUtils.Dp2Px(context, 40);//40是搜寻栏高度,是多少就写多少RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, screenHeight + topRemainHeight);marginTop = -(screenHeight + topRemainHeight - SysUtils.Dp2Px(context, imageShowHeight));layoutParams.topMargin = marginTop;iv_ad.setLayoutParams(layoutParams);

这样做的起因是,如果只把背景图设为屏幕高度,则背景图通过一直设置marginTop直至为0齐全开展时,红框局部会正好卡在底部,并不会齐全暗藏掉,起因其实很简略,如图:

图片达到底部时,因为红框与图片底部是持平的,所以正好漏在了里面,因而,这就须要下面所说的办法,将图片高度在屏幕高度根底上再+红框局部高度,这样在背景图片全屏时,可见内容区就移至了屏幕外,整个屏幕就只有背景图片可见了!

Github地址:https://github.com/baiyuliang...
原文地址:https://juejin.cn/post/702168...

文末

您的点赞珍藏就是对我最大的激励!
欢送关注我,分享Android干货,交换Android技术。
对文章有何见解,或者有何技术问题,欢送在评论区一起留言探讨!