该博客借鉴的是某位大神的博客 我只是写一下用后感和总结 博客链接地址 http://blog.csdn.net/qq_34972666/article/details/52386999?locationNum=11

首先当然是重写ViewPager啦 直接粘代码:

public class CustomViewpager extends ViewPager {
private int current;
private int height = 0;
/**
* 保存position与对于的View
*/
private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>(); private boolean scrollble = true; public CustomViewpager(Context context) {
super(context);
} public CustomViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mChildrenViews.size() > current) {
View child = mChildrenViews.get(current);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height = child.getMeasuredHeight();
} heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} public void resetHeight(int current) {
this.current = current;
if (mChildrenViews.size() > 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);
}
}
/**
* 保存position与对于的View
*/
public void setObjectForPosition(View view, int position)
{
mChildrenViews.put(position, view);
} @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;
} }

这里应该注意的是

resetHeight()的方法要根据自己实际项目的父布局来写LinearLayout还是RelativeLayout

setObjectForPosition()方法中是为了调用存放你的view和他对应的position

然后就是我们的的Fragment的代码 我们创建的Fragment的时候需要在构造方法中传入我们自定义的ViewPager对象  然后在onCreateView的方法中调用
setObjectForPosition() 下面吧Fragment的代码粘出来
public class NewPersonDataAssetPage extends BaseFragment {

    private CustomPersonViewPager vp;
private Activity activity; public NewPersonDataAssetPage(Activity activity, CustomPersonViewPager vp) {
this.activity = activity;
this.vp = vp;
} @Override
protected View initView(LayoutInflater inflater, ViewGroup container) {
View view = View.inflate(activity, R.layout.fragment_new_person_asset_page, null);
vp.setObjectForPosition(view, 1);
return view;
} @Override
protected void initData() { } @Override
public void onClick(View v) { }
}

最后就是要在ViewPager滑动的监听里面去写

activityScdetailsBottomVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override
public void onPageSelected(int position) {
activityScdetailsBottomVp.resetHeight(position); if (position == 0) { activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_1);
activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#ffffff"));
activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#c1c1c1"));
activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#c1c1c1")); } else if (position == 1) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
// activityScdetailsBottomVp.resetHeight(2);
// } else {
// activityScdetailsBottomVp.resetHeight(1);
// }
activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_2);
activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#c1c1c1"));
activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#ffffff"));
activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#c1c1c1"));
} else { activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_3);
activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#c1c1c1"));
activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#c1c1c1"));
activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#ffffff"));
}
}
@Override
public void onPageScrollStateChanged(int state) { }
});
activityScdetailsBottomVp.resetHeight(0);

关于ViewPager高度自适应(随着pager页的高度改变Viewpager的高度)的更多相关文章

  1. 解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)

    在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息 1.首先,在主页面上使用iframe引入子页面:也就是A.h ...

  2. ViewPager不能高度自适应?height=wrap_content 无效解决办法

    ViewPager用的很多,主要用啦展示广告条.可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的.. 解决办 ...

  3. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  4. iframe高度自适应

    前两天在网上看到了一道面试题,问iframe高度自适应的问题.发现自己之前几乎没有关注过iframe的问题,所以在这里记录一下. 原题目是: 页面A的域名是:http://www.taobao.com ...

  5. WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应

    WPF设置DataGrid行内容高度自适应  TextBox/TextBlock内容高度自适应  参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...

  6. JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)

    参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...

  7. 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】

    Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...

  8. iframe的高度自适应

    http://www.cnblogs.com/snandy/p/3902337.html http://www.cnblogs.com/snandy/p/3900016.html Snandy Sto ...

  9. 同域iframe的高度自适应

    引子 父页面里控制子页面 子页面里控制父页面 一.引子 我们先看一个示例,有两个页面,1.html通过iframe嵌入2.html,两个页面都是同域的 1.html <!DOCTYPE html ...

  10. extjs tablepanel 高度自适应有关问题

    extjs tablepanel 高度自适应问题 项目中为了给客户好点的功能切换体验,想到了用extjs的tabpanel 在页面中用了tabpanel后,高度新打开的tab页的iframe 的高度总 ...

随机推荐

  1. ffmpeg full help

    Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile ...

  2. LoadRunner中两种录制模式的区别

    决定我们成为什么样人的,不是我们的能力,而是我们的选择. ——<哈利-波特与密室> 一.先看看两种模式的设置和录制脚本的区别 设置HTML录制模式: 设置URL录制模式: HTML脚本: ...

  3. 安装asterisk以及asterisk-gui

           asterisk的安装在ubuntu上自我感觉还是很方便的,虽然也会遇到一些小的问题.下面是本人遇到的   一些问题和解决方法.     1>在ubuntu10.04上安装aste ...

  4. hadoop2.X集群安装与应用

    可参考此文档:hadoop(2.x)以hadoop2.2为例完全分布式最新高可靠安装文档(非常详细)http://www.aboutyun.com/thread-7684-1-1.html 步骤一:下 ...

  5. [CROATIAN2009] OTOCI

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1180 [算法] 动态树维护森林连通性 时间复杂度 : O(NlogN ^ 2) [代 ...

  6. 激活层和pooling的作用

    激活层: 激活函数其中一个重要的作用是加入非线性因素的,将特征映射到高维的非线性区间进行解释,解决线性模型所不能解决的问题 pooling层: 1. invariance(不变性),这种不变性包括tr ...

  7. bzoj 2179 FFT快速傅立叶 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2179 默写板子,注释的是忘记的地方. 代码如下: #include<iostream& ...

  8. 使用 DDMenuController 类的方法(非常好用的抽屉类)

    关于使用 DDMenuController 类的方法笔记:参考 DDMenuController 是一款非常好用的抽屉类文件. #pragma mark - 界面配置左右导航条的按钮 //[self ...

  9. TypeScript完全解读(26课时)_11.TypeScript完全解读-类型推论和兼容性

    11.TypeScript完全解读-类型推论和兼容性 在一些时候省略指令,ts会帮我们推断出省略的类型的地方适合的类型,通过学习ts的类型推论了解ts的推论规则 类型兼容性就是为了适应js灵活的特点, ...

  10. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...