两个ViewPager联动效果

说明:滑动一个ViewPager另一个跟着滑动

WrapContentHeightViewPager.java

package com.yundi.piano.ceshidemo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class WrapContentHeightViewPager extends ViewPager {
    private int current;
    private int height = 0;
    private boolean scrollble = true;

    public WrapContentHeightViewPager(Context context) {
        super(context);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getChildCount() > current) {
            View child = getChildAt(current);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            height = h;

        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void resetHeight(int current) {
        this.current = current;
        if (getChildCount() > 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);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!scrollble) {
            return true;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return scrollble && super.onInterceptTouchEvent(ev);
    }

    public boolean isScrollble() {
        return scrollble;
    }

    public void setScrollble(boolean scrollble) {
        this.scrollble = scrollble;
    }

}
BaseLinkPageChangeListener.java
package com.yundi.piano.ceshidemo;

import android.support.v4.view.ViewPager;

public class BaseLinkPageChangeListener implements ViewPager.OnPageChangeListener {

    private ViewPager linkViewPager;
    private ViewPager selfViewPager;

    private int pos;

    public BaseLinkPageChangeListener(ViewPager selfViewPager, ViewPager linkViewPager) {
        this.linkViewPager = linkViewPager;
        this.selfViewPager = selfViewPager;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        int marginX = ((selfViewPager.getWidth() + selfViewPager.getPageMargin()) * position
                + positionOffsetPixels) * (linkViewPager.getWidth() + linkViewPager.getPageMargin()) / (
                selfViewPager.getWidth()
                        + selfViewPager.getPageMargin());

        if (linkViewPager.getScrollX() != marginX) {
            linkViewPager.scrollTo(marginX, 0);
        }
    }

    @Override
    public void onPageSelected(int position) {
        this.pos = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            linkViewPager.setCurrentItem(pos);
        }
    }
}

activity_main.xml




    

        

        

        
    

MainActivity.java

package com.yundi.piano.ceshidemo;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private WrapContentHeightViewPager mVp1,mVp2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVp1=findViewById(R.id.body_vp);
        mVp2=findViewById(R.id.header_vp);
        List list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add("第" + i + "个View");
        }
        mVp1.setAdapter(new MyPagerAdapter1(MainActivity.this, list));

        List list2 = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list2.add("第" + i + "个Views");
        }
        mVp2.setAdapter(new MyPagerAdapter1(MainActivity.this, list2));

        mVp1.addOnPageChangeListener(new BaseLinkPageChangeListener(mVp1, mVp2) {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
//                pageScrollToTop();
                mVp1.resetHeight(position);//设置viewpager高度
                mVp2.resetHeight(position);
            }
        });
        mVp2.addOnPageChangeListener(new BaseLinkPageChangeListener(mVp2, mVp1) {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
//                tabLayout.onPageSelected(position);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
//                tabLayout.onPageScrolled(position, positionOffset, positionOffsetPixels);
                mVp1.resetHeight(position);
                mVp2.resetHeight(position);
            }
        });

    }
}

 

你可能感兴趣的:(Android开发,自用)