NestedScrollView 嵌套ViewPageer,ViewPager中嵌套多個帶RecyclerView的自定義view滑動時的問題解決

遇到的問題描述:java

嵌套後ViewPager中的view展現不出來,解決辦法:從新設置Viewpager的高度ide

自定義viewpagerthis

public class WrapViewPager extends ViewPager {

    private int current;
    private int height = 0;
    /**
     * 保存position與對於的View
     */
    private HashMap<Integer, View> childrenViews = new LinkedHashMap();

    private boolean isCanScroll = false;

    public WrapViewPager(Context context) {
        super(context, null);
    }

    public WrapViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

//切換tab的時候從新設置viewpager的高度 public void resetHeight(int current) { this.current = current; if (childrenViews.size() > current) { LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams(); if (layoutParams == null) { layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height); } else { layoutParams.height = height; } setLayoutParams(layoutParams); } } /** * 保存position與對於的View */ public void setObjectForPosition(View view, int position) { childrenViews.put(position, view); } @Override public void scrollTo(int x, int y) { super.scrollTo(x, y); } @Override public boolean onTouchEvent(MotionEvent arg0) { try { if (isCanScroll) { return super.onTouchEvent(arg0); } } catch (Exception e) { } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent arg0) { try { if (isCanScroll) { return super.onInterceptTouchEvent(arg0); } } catch (Exception e) { } return false; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (childrenViews.size() > current) { View child = childrenViews.get(current); child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); height = child.getMeasuredHeight(); } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }

  調用的方法:建立三個自定義view,添加到view列表中spa

view1 = UserView(context) viewpager.setObjectForPosition(goodView,0) view2 = UserView(context) viewpager.setObjectForPosition(middleView,1) view3 = UserView(context) viewpager.setObjectForPosition(badView,2)
pageList.add(view1)
pageList.add(view2)
pageList.add(view3)

監聽viewpagercode

 viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(p0: Int) { } override fun onPageScrolled(p0: Int, p1: Float, p2: Int) { } override fun onPageSelected(index: Int) { viewpager.resetHeight(index) //本身的代碼實現
 } })

設置好後高度問題解決了,因自定義view爲了防止tab切換時每次從新加載頁面和數據,作了return處理,這裏viewpager高度變化後,blog

NestedScrollView 的高度也許變化,監聽是否到底部的事件得從新監聽,放到return以前便可事件

scrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (scrollY == v.getChildAt(0).measuredHeight - v.measuredHeight) { //到底了
                if (isLoadMore) { } else { } } })
if (isLoading) {    return}
相關文章
相關標籤/搜索