ViewPager 高度自适应

2020-05-13  本文已影响0人  Android刘东

经测试可用

注意: (LayoutInflater.from(this).inflate(R.layout.xx, ViewGroup,false));
//第二个参数一定要传,否则高度不对

1.MyViewPager

package demo.app.com.horizontalscrolldemo;

import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

public class MyViewPager extends ViewPager {
    public MyViewPager(@NonNull Context context) {
        super(context);
    }

    public MyViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mChildrenCount = getChildCount();
        //遍历
        int  height=0;
        for (int i = 0; i < mChildrenCount; i++) {
            LayoutParams childLayoutParams =(LayoutParams) getChildAt(i).getLayoutParams();//获取LayoutParams
            //MeasureSpec三种模式 MeasureSpec.UNSPECIFIED:不对View大小做限制,如:ListView,ScrollView
            //MeasureSpec.EXACTLY:确切的大小,如:100dp或者march_parent
            //MeasureSpec.AT_MOST:大小不可超过某数值,如:wrap_content
            getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childLayoutParams.width,MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(childLayoutParams.height,MeasureSpec.EXACTLY));
            //取最大值
            if (height<MeasureSpec.makeMeasureSpec(getChildAt(i).getLayoutParams().height,MeasureSpec.EXACTLY)){
                height=MeasureSpec.makeMeasureSpec(getChildAt(i).getLayoutParams().height,MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(widthMeasureSpec, height);
    }


}

1.Activity

package demo.app.com.horizontalscrolldemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;

import com.zhisheng.test.R;

import java.util.ArrayList;

public class MyActivity extends Activity {
    demo.app.com.horizontalscrolldemo.MyViewPager mViewPager;
    ArrayList<View> views=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inflate);
        mViewPager = findViewById(R.id.mViewPager);
        views.add(LayoutInflater.from(this).inflate(R.layout.inflate, mViewPager,false));//第二个参数一定要传,否则高度不对
        views.add(LayoutInflater.from(this).inflate(R.layout.inflate, mViewPager,false));
        views.add(LayoutInflater.from(this).inflate(R.layout.inflate, mViewPager,false));
        mViewPager.setAdapter(new MyPagerAdapter(views,this));
    }

    class MyPagerAdapter extends PagerAdapter {
        ArrayList<View> list = new ArrayList<>();
        Context context;
        public MyPagerAdapter(ArrayList<View> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }
    }
}

3.item.xml例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="#000000"
    android:orientation="vertical">


</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读