From cb3984385969d0667562e5588456e83b7bbb1be6 Mon Sep 17 00:00:00 2001 From: teach Date: Wed, 27 May 2020 21:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86ConsecutiveViewPager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewPagerActivity.java | 17 +++++++-- .../ConsecutiveScrollerLayout.java | 21 ++++++++-- .../ConsecutiveViewPager.java | 38 ++++++++++++++++++- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java index f8ff4c0..b8a44b0 100644 --- a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java +++ b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java @@ -3,17 +3,21 @@ import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; -import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; +import com.donkingliang.consecutivescroller.ConsecutiveViewPager; import com.donkingliang.consecutivescrollerdemo.adapter.TabPagerAdapter; import java.util.ArrayList; import java.util.List; + public class ViewPagerActivity extends AppCompatActivity { + private ConsecutiveViewPager viewPager; + private TabLayout tabLayout; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -24,10 +28,17 @@ protected void onCreate(Bundle savedInstanceState) { "下面的例子中,通过自定义ViewPager,实现IConsecutiveScroller接口,ConsecutiveScrollerLayout能正确的处理ViewPager里" + "的子布局。如果ViewPager的内容是可以垂直滑动的,请使用ConsecutiveScrollerLayout或者RecyclerView等可滑动布局作为它内容的根布局。\n" + "下面的列子中使用ViewPager承载多个Fragment,Fragment的根布局为ConsecutiveScrollerLayout。"); - ViewPager viewPager = findViewById(R.id.viewPager); - TabLayout tabLayout = findViewById(R.id.tabLayout); + viewPager = findViewById(R.id.viewPager); + tabLayout = findViewById(R.id.tabLayout); viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), getTabs(), getFragments())); tabLayout.setupWithViewPager(viewPager); + + tabLayout.post(new Runnable() { + @Override + public void run() { + viewPager.setAdjustHeight(tabLayout.getHeight()); + } + }); } private List getTabs() { diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java index 35aaa84..cc4c9c2 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java @@ -827,9 +827,15 @@ private void scrollUp(int offset) { } scrollOffset = 0; + if (!isScrollBottom()) { // 找到当前显示的第一个View - View firstVisibleView = findFirstVisibleView(); + View firstVisibleView = null; + if (getScrollY() < mScrollRange) { + firstVisibleView = findFirstVisibleView(); + } else { + firstVisibleView = getBottomView(); + } if (firstVisibleView != null) { awakenScrollBars(); int bottomOffset = ScrollUtils.getScrollBottomOffset(firstVisibleView); @@ -990,10 +996,8 @@ public void checkLayoutChange(boolean changed, boolean isForce) { mAdjust = 0; checkTargetsScroll(true, isForce); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { resetChildren(); resetSticky(); -// } } /** @@ -1043,6 +1047,9 @@ private void checkTargetsScroll(boolean isLayoutChange, boolean isForce) { for (int i = index + 1; i < getChildCount(); i++) { final View child = getChildAt(i); if (ScrollUtils.isConsecutiveScrollerChild(child)) { + if (i == getChildCount() - 1 && child instanceof ConsecutiveViewPager && getScrollY() >= mScrollRange) { + continue; + } if (child instanceof IConsecutiveScroller) { List views = ((IConsecutiveScroller) child).getScrolledViews(); if (views != null && !views.isEmpty()) { @@ -1051,7 +1058,6 @@ private void checkTargetsScroll(boolean isLayoutChange, boolean isForce) { scrollChildContentToTop(views.get(c)); } } - } else { scrollChildContentToTop(child); } @@ -1191,6 +1197,13 @@ private void stopScroll() { stopNestedScroll(ViewCompat.TYPE_NON_TOUCH); } + private View getBottomView() { + if (getChildCount() > 0) { + return getChildAt(getChildCount() - 1); + } + return null; + } + /** * 返回所有的非GONE子View * diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager.java index 74ab213..8700039 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager.java @@ -6,6 +6,7 @@ import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.View; +import android.view.ViewParent; import java.util.ArrayList; import java.util.List; @@ -17,6 +18,8 @@ */ public class ConsecutiveViewPager extends ViewPager implements IConsecutiveScroller { + private int mAdjustHeight; + public ConsecutiveViewPager(@NonNull Context context) { super(context); } @@ -25,6 +28,39 @@ public ConsecutiveViewPager(@NonNull Context context, @Nullable AttributeSet att super(context, attrs); } + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (isConsecutiveParent() && mAdjustHeight > 0) { + ConsecutiveScrollerLayout layout = (ConsecutiveScrollerLayout) getParent(); + int parentHeight = layout.getMeasuredHeight(); + int height = Math.min(parentHeight - mAdjustHeight, getDefaultSize(0, heightMeasureSpec)); + super.onMeasure(widthMeasureSpec, + View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.getMode(heightMeasureSpec))); + } else { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + + private boolean isConsecutiveParent() { + ViewParent parent = getParent(); + if (parent instanceof ConsecutiveScrollerLayout) { + ConsecutiveScrollerLayout layout = (ConsecutiveScrollerLayout) parent; + return layout.indexOfChild(this) == layout.getChildCount() - 1; + } + return false; + } + + public int getAdjustHeight() { + return mAdjustHeight; + } + + public void setAdjustHeight(int adjustHeight) { + if (mAdjustHeight != adjustHeight) { + mAdjustHeight = adjustHeight; + requestLayout(); + } + } + /** * 返回当前需要滑动的view。 * @@ -55,8 +91,6 @@ public List getScrolledViews() { for (int i = 0; i < count; i++) { views.add(getChildAt(i)); } - } else { - views.add(this); } return views; }