ListView控件在片段ViewPager滚动型为Android> = 2.3 [英] ListView in Fragment in ViewPager in ScrollView for android >=2.3

查看:248
本文介绍了ListView控件在片段ViewPager滚动型为Android> = 2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能做到这一点?

Is it possible to do it?

主要的一点是:

  • 在一个复杂的头出来的列表视图但必须滚动
  • 在一个ListView的滚动由远滚动型父处理。
  • 的ListView中必须有循环的竞争,所以LinearLayout中不能成为溶液

感谢

推荐答案

在这里是封闭的ListView内滚动型的问题。下面给出的步骤描述了如何封装的任意的垂直滚动视图到另一个垂直滚动视图。

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.

第1步

写一个方法,它确定wheather视图可以垂直滚动,并放置一个常用的实用工具类中的方法如下。这种方法是从ViewPager.java和修改,以找出是否一个视图可以垂直滚动。

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);
}

第2步

子类封闭的垂直滚动视图,它可能是滚动型或ListView(在你的情况下,它是滚动型)等,并覆盖onInterceptTouchEvent()方法,如下

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);
}

第3步

子类中的封闭垂直滚动视图,它可能是GridView控件或ListView或类似的(在你的情况的ListView ),并覆盖onMeasure()方法,如下所示。无需重写此方法,滚动型。它的默认实现的行为以正确的方式。

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);
    }
}

步骤4

最后,创建一个XML布局文件,并使用滚动型和ListView,你子类,而您必须硬code中的 layout_height 。如果你是,C的高度则很难$ C $按的LayoutParams 创建通过Java code中的视图层次。这种硬编码不是必要的,如果你用你自己的测量策略,而不是一个步骤3中指定。

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.

有关更多详情,请查看这篇文章 ScrollInsideScroll ,下载该项目并检查code。

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

这篇关于ListView控件在片段ViewPager滚动型为Android&GT; = 2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆