Skip to content

Commit

Permalink
处理ConsecutiveViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed May 27, 2020
1 parent 814c0fe commit cb39843
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String> getTabs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
// }
}

/**
Expand Down Expand Up @@ -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<View> views = ((IConsecutiveScroller) child).getScrolledViews();
if (views != null && !views.isEmpty()) {
Expand All @@ -1051,7 +1058,6 @@ private void checkTargetsScroll(boolean isLayoutChange, boolean isForce) {
scrollChildContentToTop(views.get(c));
}
}

} else {
scrollChildContentToTop(child);
}
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@
*/
public class ConsecutiveViewPager extends ViewPager implements IConsecutiveScroller {

private int mAdjustHeight;

public ConsecutiveViewPager(@NonNull Context context) {
super(context);
}
Expand All @@ -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。
*
Expand Down Expand Up @@ -55,8 +91,6 @@ public List<View> getScrolledViews() {
for (int i = 0; i < count; i++) {
views.add(getChildAt(i));
}
} else {
views.add(this);
}
return views;
}
Expand Down

0 comments on commit cb39843

Please sign in to comment.