关于ViewPager高度自适应(随着pager页的高度改变Viewpager的高度)
该博客借鉴的是某位大神的博客 我只是写一下用后感和总结 博客链接地址 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的高度)的更多相关文章
- 解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)
在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息 1.首先,在主页面上使用iframe引入子页面:也就是A.h ...
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
ViewPager用的很多,主要用啦展示广告条.可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的.. 解决办 ...
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- iframe高度自适应
前两天在网上看到了一道面试题,问iframe高度自适应的问题.发现自己之前几乎没有关注过iframe的问题,所以在这里记录一下. 原题目是: 页面A的域名是:http://www.taobao.com ...
- WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
- JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...
- iframe的高度自适应
http://www.cnblogs.com/snandy/p/3902337.html http://www.cnblogs.com/snandy/p/3900016.html Snandy Sto ...
- 同域iframe的高度自适应
引子 父页面里控制子页面 子页面里控制父页面 一.引子 我们先看一个示例,有两个页面,1.html通过iframe嵌入2.html,两个页面都是同域的 1.html <!DOCTYPE html ...
- extjs tablepanel 高度自适应有关问题
extjs tablepanel 高度自适应问题 项目中为了给客户好点的功能切换体验,想到了用extjs的tabpanel 在页面中用了tabpanel后,高度新打开的tab页的iframe 的高度总 ...
随机推荐
- 支持源码单步调试QT库编译笔记
支持源码单步调试QT库编译笔记 编译环境:windows 10 编译工具:mingw_4_4_0 Qt源码版本:qt-everywhere-opensource-src-4.8.5(下载地址:http ...
- 并不对劲的BJOI2019
一些感想 现实并非游戏,并不支持反复刷关 猎人和防御工事一起被老山龙摧毁了: 猎人惨死雨中,结云村永无放晴之日: 猎人被狂龙病毒侵蚀,天空山上黑蚀龙泛滥. 好像这才是怪物猎人系列的真实结局呢 day ...
- SPOJ Find the max XOR value(二进制,贪心即可)
You have two integers L and R, and you are required to find the max xor value of a and b where L < ...
- OCX控件避免弹出安全警告的类
1.要加一个头文件: #include <objsafe.h>2.在控件头文件中加入: 1 DECLARE_INTERFACE_MAP()2 BEGIN_INTERFACE ...
- UNP总结 Chapter 11 名字与地址转换
本章讲述在名字和数值地址间进行转换的函数:gethostbyname和gethostbyaddr在主机名字与IP地址间进行转换,getservbyname和getservbyport在服务器名字和端口 ...
- salt api
#!/usr/bin/env python # -*- coding:utf8 -*- # __author__ = '北方姆Q' import requests import json from d ...
- 【220】◀▶ IDL 数组操作函数说明
参考:Array Creation Routines —— 创建数组函数参考:Array Manipulation Routines —— 操作数组函数 01 MAX 最大值. 02 MIN ...
- A. Launch of Collider (#363 Div.2)
A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- CPython里的GIL
GIL不是Python特性,是CPython解释器特性,因为CPython有垃圾回收机制. GIL 本质是互斥锁,保护解释器安全. 保证线程安全,垃圾回收线程不会和其他线程一起运行. 多个线程不能实现 ...
- Subresource Integrity(子资源一致性)和JS DDos 攻击
以下文章转载自 http://www.cnblogs.com/zoucaitou/p/4505483.html 和 http://www.puronglong.com/blog//2015/04/12 ...