如果要像我那樣顯示全的話蛤虐,需要自己定義修改下ViewPager的代碼
CustomViewPager的代碼如下
public class CustomViewPager extends ViewPager {
private Map<Integer, View> 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
* 創(chuàng)建時間:2017/11/8
* 注釋描述:測量高度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (map.size() > currentPage) {
View view = map.get(currentPage);//獲取當(dāng)前視圖的view
view.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));//測量當(dāng)前view的寬高醉箕,高度就是測量的實際大小值
height = view.getMeasuredHeight();//獲取測試的高度
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 作者:GaoXX
* 創(chuàng)建時間: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 當(dāng)前tab的高度
*/
public void addHeight(int current, View view) {
map.put(current, view);
}
}
這里主要是etLayoutParams(params);這個方法會重新調(diào)用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;//參團(tuán)用戶
private int currentTab = 0;
private List<View> 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();//參團(tuán)用戶
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
* 創(chuàng)建時間: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 = "<h4>一角寸、保證金的繳納:</h4>\n" +
"<p style=\"text-indent: 2em\">1专筷、保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納。</p>\n" +
"<p style=\"text-indent: 2em\">2撕攒、保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納陡鹃。</p>\n" +
"<p style=\"text-indent: 2em\">3、保證金的繳納保證金的繳納保證金的繳納保證金的繳納:</p>\n" +
"<p style=\"text-indent: 4em\">3.1保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納抖坪,平臺運(yùn)營客服后臺確認(rèn)后即可參團(tuán)萍鲸。</p>\n" +
"<p style=\"text-indent: 4em\">3.2保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納。</p>\n" +
"<p style=\"text-indent: 4em\">3.3保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納擦俐。</p>\n" +
"<h4>二脊阴、保證金的扣除: </h4>\n" +
"<p style=\"text-indent: 2em\">1、保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納</p>\n" +
"<h4>三蚯瞧、保證金的退還:</h4>\n" +
"<p style=\"text-indent: 2em\">1嘿期、保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納保證金的繳納;</p>\n" +
"<p style=\"text-indent: 2em\">2埋合、如您繳納的保證金逾期未予以退還秽五,。</p>\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
* 創(chuàng)建時間:2017/8/18
* 注釋描述:參團(tuán)用戶
*/
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);
}
/**
* 這個方法會在當(dāng)前頁面發(fā)生滾動的時候被調(diào)用
* @param position 當(dāng)前頁面的索引
* @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
* 創(chuàng)建時間:2017/8/18
* 注釋描述:初始化購買用戶的apdater
*/
static class ShoppBuyUserAdapter extends PagerAdapter {
private List<View> viewList;
public ShoppBuyUserAdapter(List<View> 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));
}
}
}