Android ScrollView, scroll to visible

How can I set the scrollview scroll to x pixels before it appears?

In ScrollView, I have some views, and I know that it will fill the screen. Can I scroll to enable the second view so that the first view is not visible when the activity starts?

Now I have it, but I think it is not perfect, sometimes I see the first view, and then it scrolls to the second.

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

CHANGE !!

The 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. 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 at the moment we need to configure scrolling. Therefore, 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 never works for me.

+6
source share
6 answers

I combined onGlobalLayout with the code below. It's not so great to use this hack, but it is still pretty effective. When I get the onGlobalLayout event, I check if the layouts are complete, and if that is normal, I use the 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); } 
+2
source

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

+1
source

I solved this problem 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()); } } } 
+1
source

The right way to do this is to call scrollTo () in the ScrollView after the view has gone through its measurement and layouts, otherwise the content width in scrollView will be 0, and thus scrollTo () will not go to the right position. Usually after calling the constructor and after adding the view to its parent object.

See http://developer.android.com/guide/topics/ui/how-android-draws.html to find out how android draws views.

0
source

MrJre gives the most elegant solution: "override onLayout () in scrollview and do it there after super.onLayout ()"

Sample code here:

fooobar.com/questions/51881 / ...

0
source

Using:

 scrollTo (int x, int y) It sets the scrolled position of your view. This will cause a call to onScrollChanged 

Details: see this

ScrollView ScrollTo

-1
source

Source: https://habr.com/ru/post/895309/


All Articles