Android ScrollView,在可见之前滚动

时间:2011-08-18 09:03:55

标签: android

在如何显示之前,如何将scrollview的滚动设置为x像素?

在ScrollView中我有一些视图,我知道它会填满屏幕。我可以滚动来设置第二个视图,以便在活动开始时第一个视图不可见吗?

现在我有这样的感觉,但我认为它并不完美,有时,我看到第一个View,然后滚动到第二个

@Override
public void onGlobalLayout() {
    if (mHorizontalScrollView.getChildCount() > 0 && mHorizontalScrollView.getChildAt(0).getWidth() > mScreenWidth) {
            hsv.scrollTo(100, 0);
    }
}

EDIT !!

第二次尝试是使用http://developer.android.com/reference/android/view/ViewTreeObserver.OnPreDrawListener.html代替http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.htmlOnPreDrawListener我们可以读到At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.所以基本上我们应该调整滚动。所以我创建了:

@Override
public boolean onPreDraw() {
    if (hsv.getChildCount() > 0 && hsv.getChildAt(0).getWidth() > mScreenWidth) {
        hsv.scrollTo(100, 0);
        return false;
    }
    return true;
}

但现在它永远不适合我。

6 个答案:

答案 0 :(得分:2)

我将onGlobalLayout与以下代码合并。使用该黑客并不是那么好,但它仍然非常有效。当我进入GlobalLayout事件时,我会检查布局是否已完成,然后如果可以,我会使用下面的方法。

// hsv - horizontal scroll view
private void forceShowColumn(final int column) {
    int scrollTo = childViewWidth * column;
    if (scrollTo == hsv.getScrollX()) {
        return;
    }
    hsv.scrollTo(scrollTo, 0);

    hsv.postDelayed(new Runnable() {

        @Override
        public void run() {
            forceShowColumn(column);
        }
    }, 10);
}

答案 1 :(得分:1)

在onCreate-method中尝试View.post(Runnable)。

答案 2 :(得分:1)

我通过在控制ScrollView的片段中实现View.OnLayoutChangeListener解决了这个问题。

#include <stdio.h>

#define max(a, b) (((a)>(b))?(a):(b));
int get_max(int *a, int i, int size)
{
    if (i >= size)
        return 0;
    return max(a[i], get_max(a, i+1, size));
}

int get_sum(int *a, int i, int size)
{
    if (i >= size)
        return 0;
    return a[i] + get_sum(a, i+1, size);
}

int get_partition(int *a, int size, int bound) {
    int running_sum = 0;
    int partitions = 0, i;

    for (i=0;i<size;i++) {
        if (a[i] + running_sum <= bound) {
            running_sum += a[i];
        } else {
            running_sum = 0;
            running_sum += a[i];
            partitions++;
        }
    }
    if (running_sum > 0)
        partitions++;
    return partitions;
}

int foo(int *a, int size, int k)
{
    int lower = get_max(a, 0, size);
    int higher = get_sum(a, 0, size);
    int partition;

    while (lower+1 < higher) {
        int bound = (lower + (higher))/2;

        partition = get_partition(a, size, bound);
        printf("partition %d bound %d lower %d higher %d\n", partition, bound, lower, higher);
        if (partition > k)
            lower = bound;
        else
            higher = bound;
    }
    printf("partition %dlower %d higher %d\n", partition, lower, higher);
    return higher;
}

#define SIZE(a) sizeof(a)/sizeof(a[0])
int main(void) {
    int a[] = {2, 3, 4, 5, 6};
    printf("%d\n", foo(a, SIZE(a), 3));
    return 0;
}

...

public class YourFragment extends Fragment implements View.OnLayoutChangeListener{

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.your_fragment_layout, container, false);

    //set scrollview layout change listener to be handled in this fragment
    sv = (ScrollView) view.findViewById(R.id.your_scroll_view);
    sv.addOnLayoutChangeListener(this);

    return view;
}

答案 3 :(得分:0)

执行此操作的正确方法是在视图通过其测量和布局传递后调用ScrollView上的scrollTo(),否则scrollView中内容的宽度将为0,因此scrollTo()将不滚动到正确的位置。通常在调用构造函数之后以及将视图添加到其父级之后。

有关android如何绘制视图的更多信息,请参阅http://developer.android.com/guide/topics/ui/how-android-draws.html

答案 4 :(得分:0)

MrJre提供了最优雅的解决方案:“在scrollview中覆盖onLayout()并在super.onLayout()后执行此操作”

此处的示例代码:

https://stackoverflow.com/a/10209457/1310343

答案 5 :(得分:-1)

使用:

scrollTo (int x, int y)

It sets the scrolled position of your view. This will cause a call to onScrollChanged

有关详细信息:请参阅此

ScrollView ScrollTo

相关问题