viewpager inside viewpager not showing [error]

you cannot generally have scrollable things inside of other scrollable things — they tend to fight over touch events.

mastery of onInterceptTouchEvent is the secret to all nested scrollable views.

1. ScrollView 包着viewPager,viewPager内容不显示问题

解决方法:xml中设置viewPager的高度和宽度.

isViewFromObject

2.能够兼容ViewPager的ScrollView

解决了ViewPager在ScrollView中的滑动反弹问题

public class ScrollViewExtend extends ScrollView {
// 滑动距离及坐标
private float xDistance, yDistance, xLast, yLast;
public ScrollViewExtend(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
xLast = curX;
yLast = curY;
if(xDistance > yDistance){
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}

3.ScrollView中嵌入ViewPager,ViewPager的滑动出现问题 

在滑动ViewPager进行切换时,必须完全保持水平的滑动。如若进行滑动的时候滑动方向发生了倾斜,就会触发ScrollView的滚动事件,而ViewPager的滑动也会终止并变回原来的样子。怎样才能让在ViewPager进行滑动时ScrollView不滚动?

public class CustomScrollView extends ScrollView {
private boolean canScroll;
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(new YScrollDetector());
canScroll = true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_UP)
canScroll = true;
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(canScroll)
if (Math.abs(distanceY) >= Math.abs(distanceX))
canScroll = true;
else
canScroll = false;
return canScroll;
}
}
}

只要在设置ListView的Adapter后调用此静态方法即可让ListView正确的显示在其父ListView的ListItem中。但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常

4.解决ViewPager双层嵌套的滑动问题

ViewFlipper控件有点死板,只能等待你手势执行完毕后才执行滑动效果,用起来很不爽,不过ViewPager可以很容易实现这个效果.但是有个缺陷,也就是在首页时候向左拽,在最后一页向右拽,这两者是没有效果的,而iphone的效果是你拖拽出现一片空的page会自动缩回首页或尾页

5.ViewPager禁止滑动翻页

ViewPager中切换到某一页时禁止当前页滑动到下一页和上一页,可以通过控制Touch事件来实现;

viewPager.setOnTouchListener(this);

public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
if(currentview==3)
{
return true;
}
return false;
}

OnPageChangeListener 里面获取当前的页面值(currentview)

ViewPager嵌套子ViewPager不能滑动

重写父ViewPager,并在父ViewPager重载onInterceptTouchEvent方法,并返回false:
    @Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent ) {
return false;
}
这样,子ViewPager就能滑动了。
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: