Android -下拉刷新,ViewPager和ScrollView嵌套滚动问题解决方案
2016-01-21 12:05
495 查看
》ViewPager和ScrollView嵌套滚动问题解决方案,即View冲突,重绘冲突的那部分的View的大小,宽度和高度,根据具体情况而定。
View视图和图层。。
下面的情况是重绘了ViewPager的高度:resetViewPagerHeight(int position)
-------------Java代码如下:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Solution3Activity extends FragmentActivity {
private ViewPager viewPager;
private ViewPagerFragmentAdapter adapter;
private MyListener listener = new MyListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_solution3);
viewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager());
adapter.addFragment(new Content1Fragment());
adapter.addFragment(new Content2Fragment());
adapter.addFragment(new Content3Fragment());
viewPager.setOffscreenPageLimit(3);
viewPager.setOnPageChangeListener(listener);
viewPager.setAdapter(adapter);
// 模拟网络请求完成之后重置ViewPager高度
new myAsyncTask().execute();
}
/**
* 重新设置viewPager高度
*
* @param position
*/
public void resetViewPagerHeight(int position)
{
View child = viewPager.getChildAt(position);
if (child != null) {
child.measure(0, 0);
int h = child.getMeasuredHeight();
LinearLayout.LayoutParams params = (LayoutParams) viewPager
.getLayoutParams();
params.height = h + 50;
viewPager.setLayoutParams(params);
}
}
public class MyListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// 页面切换后重置ViewPager高度
resetViewPagerHeight(position);
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
}
public class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
resetViewPagerHeight(0);
}
}
}
---- 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" >
<com.hellsam.scrollviewwithviewpager.CustomScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#0000FF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="很大一块内容 H:350dp"
android:textColor="#FFFFFF" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容1 H:300dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容2 H:500dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容3 H:800dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</com.hellsam.scrollviewwithviewpager.CustomScrollView>
</LinearLayout>
View视图和图层。。
下面的情况是重绘了ViewPager的高度:resetViewPagerHeight(int position)
-------------Java代码如下:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Solution3Activity extends FragmentActivity {
private ViewPager viewPager;
private ViewPagerFragmentAdapter adapter;
private MyListener listener = new MyListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_solution3);
viewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager());
adapter.addFragment(new Content1Fragment());
adapter.addFragment(new Content2Fragment());
adapter.addFragment(new Content3Fragment());
viewPager.setOffscreenPageLimit(3);
viewPager.setOnPageChangeListener(listener);
viewPager.setAdapter(adapter);
// 模拟网络请求完成之后重置ViewPager高度
new myAsyncTask().execute();
}
/**
* 重新设置viewPager高度
*
* @param position
*/
public void resetViewPagerHeight(int position)
{
View child = viewPager.getChildAt(position);
if (child != null) {
child.measure(0, 0);
int h = child.getMeasuredHeight();
LinearLayout.LayoutParams params = (LayoutParams) viewPager
.getLayoutParams();
params.height = h + 50;
viewPager.setLayoutParams(params);
}
}
public class MyListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// 页面切换后重置ViewPager高度
resetViewPagerHeight(position);
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
}
public class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
resetViewPagerHeight(0);
}
}
}
---- 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" >
<com.hellsam.scrollviewwithviewpager.CustomScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#0000FF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="很大一块内容 H:350dp"
android:textColor="#FFFFFF" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容1 H:300dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容2 H:500dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="内容3 H:800dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</com.hellsam.scrollviewwithviewpager.CustomScrollView>
</LinearLayout>
相关文章推荐
- 阿冰教你一步一步做Android新闻客户端(二)两种异步线程加载图片的方法
- android安卓屏蔽禁用系统输入法,自定义软键盘,解决EditText光标问题demo
- Android Parcelable和Serializable的区别
- android 通过字符串来获取R下面资源的ID 值
- 【Android 基础】Animation 动画介绍和实现
- Android基础Activity初步了解
- Android拨打电话(Intent.ACTION.CALL)
- Android Studio中JNI -- 2 -- 编写c文件
- 深入android6.0 设备 idle状态
- Android6.0 设备Idle状态(二)AlarmManagerService setIdleUntil接口
- [Androd初级]解决Listview的子项Item的高度无法设置的情节
- Android性能优化典范
- Android之TextView实现文字过长时省略部分或者滚动显示
- android 部分 FLAG释义
- Android 二:root 手机的原理
- Android开发中无处不在的设计模式——动态代理模式
- Android 一:通过adb命令判断手机是否被root过
- Android PopupWindow 疑难杂症之宽度WRAP_CONTENT
- android编译系统学习
- Huffman编码译码器 Android版本