ScrollView嵌套listView和Viewpager后的显示不全解决
2015-12-07 17:53
429 查看
由于项目要求,需要在Viewpager中有一个是片段是ScrollView,而scrollView中需要嵌套另外的ViewPager跟其他如listView的视图,这里先解决下scrollView中嵌套ViewPager与listView后显示不全的问题。
xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.scrollviewandother.MyListView
android:id="@+id/lv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.scrollviewandother.MyListView>
<com.example.scrollviewandother.MyViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.scrollviewandother.MyViewPager>
</LinearLayout>
</ScrollView>
</LinearLayout>
自定义listView,重写他的onMeasure方法:解决listView显示不全
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
自定义ViewPager,重写他的onMeasure方法:[b]解决viewPager显示不全[/b]
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int viewHeight = 0;
View childView = getChildAt(getCurrentItem());
if(childView!=null){
childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewHeight = childView.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(viewHeight, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
同时设置Viewpager监听:否则最后一页的数据高度不全
final int currentSelectedPosition = viewpager.getCurrentItem();
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
View view = viewpager.getChildAt(currentSelectedPosition);
int height = view.getMeasuredHeight();
LayoutParams layoutParams = (LinearLayout.LayoutParams) viewpager.getLayoutParams();
layoutParams.height = height;
viewpager.setLayoutParams(layoutParams);
}
运行效果;
为了简易所以片段跟activity都用的同一xml,上面的是listView,下面的是viewpager
xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.scrollviewandother.MyListView
android:id="@+id/lv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.scrollviewandother.MyListView>
<com.example.scrollviewandother.MyViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.scrollviewandother.MyViewPager>
</LinearLayout>
</ScrollView>
</LinearLayout>
自定义listView,重写他的onMeasure方法:解决listView显示不全
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
自定义ViewPager,重写他的onMeasure方法:[b]解决viewPager显示不全[/b]
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int viewHeight = 0;
View childView = getChildAt(getCurrentItem());
if(childView!=null){
childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewHeight = childView.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(viewHeight, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
同时设置Viewpager监听:否则最后一页的数据高度不全
final int currentSelectedPosition = viewpager.getCurrentItem();
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
View view = viewpager.getChildAt(currentSelectedPosition);
int height = view.getMeasuredHeight();
LayoutParams layoutParams = (LinearLayout.LayoutParams) viewpager.getLayoutParams();
layoutParams.height = height;
viewpager.setLayoutParams(layoutParams);
}
运行效果;
为了简易所以片段跟activity都用的同一xml,上面的是listView,下面的是viewpager
相关文章推荐
- 读书不如行路
- Eclipse中的Tomcat关闭了但还是显示Started或Debugging
- [BZOJ1776] [Usaco2010 Hol]cowpol 奶牛政坛
- mac svn
- base64与图片之间的转换
- PostgreSQL表空间
- UIView常用属性以及方法
- Huffman学习记录 c语言
- [Javascript Data Structures] 冒泡排序 bubble sort
- 移动端与pc端公用一个域名
- C++类中定义线程
- 表格的变色问题
- 工作流的学习(四)
- waiting for table metadata lock 问题深入分析
- linux sort,uniq,cut,wc,tr命令详解
- Python 元组简单用法
- 工作环境搭建(1) - CentOS7虚拟机的最小化安装
- Hadoop2.6.0学习笔记(八)InputFormat和OutputFormat
- 详解Java线程编程中的volatile关键字的作用
- 数学公式