我是靠谱客的博主 优秀未来,最近开发中收集的这篇文章主要介绍关于ViewPager高度自适应(随着页面高度改变Viewpager的高度),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

重写Viewpager

public class MyViewPager extends ViewPager {
private int current;
private int viewHeight = 0;
private boolean scrollble = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View childView = getChildAt(getCurrentItem());
if (childView != null)
//有可能没有子view
{
childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewHeight = childView.getMeasuredHeight();
//得到父元素对自身设定的高
// UNSPECIFIED(未指定),父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小
//EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
//AT_MOST(至多),子元素至多达到指定大小的值。
heightMeasureSpec = MeasureSpec.makeMeasureSpec(viewHeight, 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, viewHeight);
} else {
layoutParams.height = viewHeight;
}
setLayoutParams(layoutParams);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!scrollble) {
return true;
}
return super.onTouchEvent(ev);
}
public boolean isScrollble() {
return scrollble;
}
public void setScrollble(boolean scrollble) {
this.scrollble = scrollble;
}
}

xml文件 tablayout +viewpager

 <com.flyco.tablayout.SegmentTabLayout
android:id="@+id/tab_cd"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_margin="10dp"
tl:tl_bar_color="@color/none"
tl:tl_indicator_color="@color/red_progress"
tl:tl_indicator_corner_radius="10dp"
tl:tl_tab_padding="30dp"
tl:tl_textSelectColor="@color/white"
tl:tl_textUnselectColor="@color/gray_txt_select_color"/>
<org.gbmedia.hmall.main.update.view.MyViewPager
android:id="@+id/mvp_cd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_space_h"
android:layout_marginRight="@dimen/content_space_h"
android:layout_marginTop="@dimen/content_space_h"
android:background="@color/white"/>

activity


@ViewInject(R.id.tab_cd)
SegmentTabLayout tab_cd;
@ViewInject(R.id.mvp_cd)
MyViewPager vp_cd;
private MyViewPagerAdapter myViewPagerAdapter;
private LocalActivityManager manager;
private List<View> mViewList = new ArrayList<>();
private String[] mTitles = {"资源详情", "凑单进度"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);
vp_cd.getParent().requestDisallowInterceptTouchEvent(true);
tab_cd.setTabData(mTitles);
View tabDetail = manager.startActivity("0", intentRes).getDecorView();
mViewList.add(tabDetail);
Intent intentprogress = new Intent(this, ResCDProgressActivity.class);
intentprogress.putExtra("coudan_info", detail.coudan_info);
View tabProgress = manager.startActivity("1", intentprogress).getDecorView();
mViewList.add(tabProgress);
Intent intent = getIntent();
int page = intent.getIntExtra("page", 0);
vp_cd.setCurrentItem(page);
myViewPagerAdapter = new MyViewPagerAdapter(mViewList, mTitles);
vp_cd.setAdapter(myViewPagerAdapter);
vp_cd.setOffscreenPageLimit(0);
tab_cd.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
if (position == 0) {
ll_detail.setVisibility(View.VISIBLE);
ll_program.setVisibility(View.GONE);
} else {
ll_program.setVisibility(View.VISIBLE);
ll_detail.setVisibility(View.GONE);
}
vp_cd.setCurrentItem(position);
}
@Override
public void onTabReselect(int position) {
}
});
vp_cd.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
vp_cd.resetHeight(position);
tab_cd.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
vp_cd.resetHeight(0);
}

adapter

public class MyViewPagerAdapter extends PagerAdapter {
private List<View> mViewList;
private String[] mTitles;
public MyViewPagerAdapter(List<View> mViewList, String[] titles) {
this.mViewList = mViewList;
this.mTitles = titles;
}
@Override
public int getCount() {
return mViewList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mViewList.get(position));//添加页卡
container.setTag(position);
return mViewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));//删除页卡
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;//官方推荐写法
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];//页卡标题
}
}

最后

以上就是优秀未来为你收集整理的关于ViewPager高度自适应(随着页面高度改变Viewpager的高度)的全部内容,希望文章能够帮你解决关于ViewPager高度自适应(随着页面高度改变Viewpager的高度)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部