android2.3 fragment,ListView in Fragment in ViewPager in ScrollView for android >=2.3

The problem here is enclosing ListView inside ScrollView. The steps given below describe how to enclose any vertically scrollable view into another vertically scrollable view.

Step 1

Write a method which determines wheather a View can be scrolled vertically and place the method inside a common utility class as follows. This method is taken from ViewPager.java and modified to find whether a View can vertically scrollable.

public static boolean canScroll(View v, boolean checkV, int dy, int x, int y) {

if (v instanceof ViewGroup) {

final ViewGroup group = (ViewGroup) v;

final int scrollX = v.getScrollX();

final int scrollY = v.getScrollY();

final int count = group.getChildCount();

for (int i = count - 1; i >= 0; i--) {

final View child = group.getChildAt(i);

if (x + scrollX >= child.getLeft()

&& x + scrollX < child.getRight()

&& y + scrollY >= child.getTop()

&& y + scrollY < child.getBottom()

&& canScroll(child, true, dy,

x + scrollX - child.getLeft(), y + scrollY

- child.getTop())) {

return true;

}

}

}

return checkV && ViewCompat.canScrollVertically(v, -dy);

}

Step 2

Subclass the enclosing vertically scrollable view, it may be ScrollView or ListView(In your case it is ScrollView), or the like and override the onInterceptTouchEvent() method as follows.

public boolean onInterceptTouchEvent(MotionEvent event) {

int action = event.getAction();

float x = event.getX();

float y = event.getY();

float dy = y - mLastMotionY;

switch (action) {

case MotionEvent.ACTION_DOWN:

mLastMotionY = y;

break;

case MotionEvent.ACTION_MOVE:

if (Util.canScroll(this, false, (int) dy, (int) x, (int) y)) {

mLastMotionY = y;

return false;

}

break;

}

return super.onInterceptTouchEvent(event);

}

Step 3

Subclass the enclosed vertically scrollable view, it may be GridView or ListView or the like(In your case ListView) and override the onMeasure() method as follows. No need to override this method in ScrollView. Its default implementation behaves in the right way.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int mode = MeasureSpec.getMode(widthMeasureSpec);

if (mode == MeasureSpec.UNSPECIFIED) {

int height = getLayoutParams().height;

if (height > 0)

setMeasuredDimension(getMeasuredWidth(), height);

}

}

Step 4

Finally create an xml layout file and use the ScrollView and ListView that you subclassed and you must hard code the layout_height. If you are creating the view hierarchy through java code, then hard code the height by LayoutParams. This hard coding is not necessary if you use your own measuring strategy rather than one specified in step 3.

For further details please view this post ScrollInsideScroll, download the project and examine the code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值