Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ScrollView, scroll before visible

Tags:

android

how can I set scroll of scrollview to x pixels, before it's even shown?

In ScrollView I have some Views and i know that it will fill screen. Can I scroll to let say second View, so that first View is not visible when activity is started?

Now I have sth like this, but I think it's not perfect, sometimes, I see first View, and then it's scrolled to the second one

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

EDIT!!

Second attempt was to use http://developer.android.com/reference/android/view/ViewTreeObserver.OnPreDrawListener.html instead of http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html In OnPreDrawListener we can read that 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. so basically at this point we should adjust scrolling. So I created:

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

but now it's never working for me.

like image 260
Mikooos Avatar asked Aug 18 '11 09:08

Mikooos


People also ask

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

What is Overscroll Android?

Overscrolling effects indicate that the user has reached the end of a scrollable list or page. It informs users that there is no more content available. Overscroll effects can be added at the top, bottom, right and left sides of the screen, depending on the screen structure.


3 Answers

I combined onGlobalLayout with code below. It's not so great to use that hack but it still quite efficient. When I get onGlobalLayout event I check if layouts are done and then if it's ok I use method below.

// 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);
}
like image 68
Mikooos Avatar answered Nov 15 '22 04:11

Mikooos


try View.post(Runnable) in onCreate-method.

like image 33
hauns Avatar answered Nov 15 '22 04:11

hauns


I solved this issue by implementing View.OnLayoutChangeListener in the fragment that controls my ScrollView.

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

...

public void onLayoutChange (View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom){
    switch (v.getId())
    {
        case R.id.your_scroll_view:
        {
                sv.scrollTo(0, sv.getTop());
        }
    }
}
like image 45
sspaniel Avatar answered Nov 15 '22 03:11

sspaniel