ScrollView与ViewPager 显示不全

GIF.gif

如果要像我那样显示全的话,需要自己定义修改下ViewPager的代码

CustomViewPager的代码如下

public class CustomViewPager extends ViewPager {
    private Map map = new HashMap<>(3);
    private int currentPage;
    int height = 0;

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

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

    /**
     * 作者:GaoXX
     * 创建时间:2017/11/8
     * 注释描述:测量高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (map.size() > currentPage) {
            View view = map.get(currentPage);//获取当前视图的view
            view.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));//测量当前view的宽高,高度就是测量的实际大小值
            height = view.getMeasuredHeight();//获取测试的高度
        }

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


    /**
     * 作者:GaoXX
     * 创建时间:2017/11/8
     * 注释描述:在切换tab的时候,重置ViewPager的高度
     */
    public void resetHeight(int current) {
        this.currentPage = current;//获取切换的tab
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
        setLayoutParams(params);//会重新刷新onMeasure方法
    }

    /**
     * 获取、存储每一个tab的高度,在需要的时候显示存储的高度
     *
     * @param current tab的position
     * @param view    当前tab的高度
     */
    public void addHeight(int current, View view) {
        map.put(current, view);
    }
}

这里主要是etLayoutParams(params);这个方法会重新调用measure
MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {
    CustomViewPager myViewPager;
    TextView tvProductInformation;
    TextView tvPurchaseNotes;
    TextView tvDepartureUser;
    public static final int TAB_PRODUCT = 0;//商品信息
    public static final int TAB_PURCHASE = 1;//购买须知
    public static final int TAB_DEPARTUSER = 2;//参团用户
    private int currentTab = 0;
    private List viewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myViewPager = (CustomViewPager) this.findViewById(R.id.myViewPager);
        tvProductInformation = (TextView) this.findViewById(R.id.tv_merchandise_Product_Information);
        tvPurchaseNotes = (TextView) this.findViewById(R.id.tv_merchandise_purchase_notes);
        tvDepartureUser = (TextView) this.findViewById(R.id.tv_merchandise_DepartureUser);
        initProductInformation();//初始化商品信息
        initPurchaseNotes();//初始化购买须知
        initDepartureUser();//参团用户

        tvProductInformation.setTag(TAB_PRODUCT);
        tvPurchaseNotes.setTag(TAB_PURCHASE);
        tvDepartureUser.setTag(TAB_DEPARTUSER);

        tvProductInformation.setOnClickListener(this);
        tvPurchaseNotes.setOnClickListener(this);
        tvDepartureUser.setOnClickListener(this);

        initmvpShoppBuyUser();
        for (int i = 0; i < viewList.size(); i++) {
            myViewPager.addHeight(i, viewList.get(i));
        }
        myViewPager.resetHeight(0);
    }

    /**
     * 作者:GaoXX
     * 创建时间:2017/8/18
     * 注释描述:初始化商品信息
     */
    private void initProductInformation() {
        View viewProduct = LayoutInflater.from(this).inflate(R.layout.view_product_information, null);
        viewList.add(viewProduct);
    }

    private void initPurchaseNotes() {
        String html = "

一、保证金的缴纳:

\n" + "

1、保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳。

\n" + "

2、保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳。

\n" + "

3、保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳:

\n" + "

3.1保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳,平台运营客服后台确认后即可参团。

\n" + "

3.2保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳。

\n" + "

3.3保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳。

\n" + "

二、保证金的扣除:

\n" + "

1、保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳

\n" + "

三、保证金的退还:

\n" + "

1、保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳保证金的缴纳;

\n" + "

2、如您缴纳的保证金逾期未予以退还,。

\n"; CharSequence charSequence = Html.fromHtml(html); View viewPurchase = LayoutInflater.from(this).inflate(R.layout.view_purchase_notes, null); TextView textView = (TextView) viewPurchase.findViewById(R.id.tv_purchase_notes); textView.setText(charSequence); viewList.add(viewPurchase); } /** * 作者:GaoXiaoXiong * 创建时间:2017/8/18 * 注释描述:参团用户 */ private void initDepartureUser() { View viewDepartureUser = LayoutInflater.from(this).inflate(R.layout.view_departure_user, null); viewList.add(viewDepartureUser); } private void initmvpShoppBuyUser() { final ShoppBuyUserAdapter buyUserAdapter = new ShoppBuyUserAdapter(viewList); myViewPager.setAdapter(buyUserAdapter); myViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { tvProductInformation.setTextColor(MainActivity.this.getResources().getColor(R.color.black)); tvPurchaseNotes.setTextColor(MainActivity.this.getResources().getColor(R.color.black)); tvDepartureUser.setTextColor(MainActivity.this.getResources().getColor(R.color.black)); switch (position) { case TAB_PRODUCT: { currentTab = TAB_PRODUCT; tvProductInformation.setTextColor(MainActivity.this.getResources().getColor(R.color.blue)); } break; case TAB_PURCHASE: { currentTab = TAB_PURCHASE; tvPurchaseNotes.setTextColor(MainActivity.this.getResources().getColor(R.color.blue)); } break; case TAB_DEPARTUSER: { currentTab = TAB_DEPARTUSER; tvDepartureUser.setTextColor(MainActivity.this.getResources().getColor(R.color.blue)); } break; } myViewPager.resetHeight(position); } /** * 这个方法会在当前页面发生滚动的时候被调用 * @param position 当前页面的索引 * @param positionOffset 它取值范围为[0,1),表示偏移量。0->没有偏移 接近于1->几乎完全偏移 * @param positionOffsetPixels 偏移的像素值 */ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View view) { myViewPager.setCurrentItem((int) view.getTag(), true); } /** * 作者:GaoXX * 创建时间:2017/8/18 * 注释描述:初始化购买用户的apdater */ static class ShoppBuyUserAdapter extends PagerAdapter { private List viewList; public ShoppBuyUserAdapter(List viewList) { this.viewList = viewList; } @Override public int getCount() { return viewList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == (View) object; } @Override public View instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } } }

代码传送门

你可能感兴趣的:(ScrollView与ViewPager 显示不全)