I have a scrollView in my ViewPager fragment. I want to set the initial scrollView position to (0,100).
I tried setting scrollView.scrollTo(0,100) in onCreateView(). It didn't work.
Then I tried the same in Handler and it worked but it only scrolls to (0,100) after (0,0) causing little jerk in scrollView which is bad for user experience.
Is there any way to to make scrollview directly scroll to (0,100) instead of (0,0)? or Is there any method available in scrollView to detect initial scroll event?
P.S :I saw the question here which also the same as mine but the accepted answer is not working for me.
Any help would be appreciated.
Thanks
Instead of scrollTo you can use requestFocus on first child of inner layout of scroll view.
Example:
<ScrollView>
<LinearLayout>
<TextView
android:focusable="true"
android:focusableInTouchMode="true"
android:tag="ScrollTopTag" />
//***rest of your layout***//
</LinearLayout>
</ScrollView>
In your activity where you want to scroll to top of scroll view add below line
View view = findViewByTag("ScrollTopTag);
view.requestFocus();
It feels like you are inflating views inside the adapter without recycling them. This would explain the choppy loading times. Other than that, from the example, try delaying it even more. In the example it uses 250ms, but if your listview it not done loading than its not going to scroll anywhere since it is not filled yet. I would start off but first increasing the duration to about 2000ms just to see if it works. If that works, I would go back to your adapter class and make sure each view is recycling instead of reinflated.
Related
I am using two (embedded) ScrollViews like suggested here, in order to create a '2D Scrollview'.
I add multiple childs to this view, and to some of them I set OnClickListener (I also tried with OnLongClickListener as well).
Functionally the result is what I have expected, although if I try to scroll (starting from a child, that has either of the listeners), the scrollview jumps/repositions to the ~opposite direction, I started the scroll to. So if I scroll like this e.g. upwards, the view jumps a big downwards so that I can scroll up at most to the original position.
I have been trying to play around with calling setFocusable(false) on the childs and setFocusable(true) on the ScrollView(s) (but also tried different permutations of it, as I am not sure about setFocusable()), but couldn't really get on top of it.. Any suggestions?
Just encase you are still wondering this is happening because when you start scrolling the other scroll view, ACTION_CANCEL is posted to the on touch method of the original ScrollView which causes a default scroll view to jump back to its original position.
I have implemented this https://stackoverflow.com/a/22797619/3064486 (Vertical ViewPager) in my activity.
The fragments that my adapter puts inside the viewpager is having a scrollview in it.
When I reach the bottom of the scrollview it does not switch page in the viewpager. However, if I swipe from the right or left bottom corner it kind of works, it's a bit hard to explain but it jumps 20-30% of the full swipe (from page 1 to page 2) and if I immediately release it goes back and I can also swipe to continue the page switch.
I believe my scrollview is the problem here..
ViewPager
<com.mypackage.MyVerticalViewPager>
The fragment
<LinearLayout>
<ScrollView>
<!-- massive amount of stuff -->
</ScrollView>
</LinearLayout>
I have tried answers here:
https://stackoverflow.com/a/14435116/3064486
Edit: I tried this
https://github.com/castorflex/VerticalViewPager/blob/master/library/src/main/java/fr/castorflex/android/verticalviewpager/VerticalViewPager.java
And got the vertical scroll to work. The ScrollView also works but when I get to the bottom I need to really FLING the finger to get to the other page. I guess I have to remove some kind of overscroll for the scrollView. Anyone have any idea?
use ListView instead of ScrollView can solve this problem. since ScrollView call parent.requestDisallowInterceptTouchEvent(true) method on its onTouchEvent method, its hard to handle scroll conflicts, I am also working with this problem, I will give my solution when done.
My question is very similar to this unanswered one, with some small differences that I will explain:
TabHost inside of a Scrollview: always scrolls down when a Tab is clicked
My ScrollView initiates scrolled to the bottom, showing the TabHost content instead of the ones on top of it (you can see the screenshots in the linked question, it's very similar).
If I manually scroll up, clicking in different tabs doesn't affect the ScrollView, the problem is only in the first time it's shown. This is the difference between the other question.
Things I tried with no success:
Make the top components inside the ScrollView focusable, so they would get the focus instead of the tab.
Force the ScrollView to scroll to the top in onResume() method with sv.fullScroll(View.FOCUS_UP); or sv.scrollTo(0, 0);. Also, no luck in the initialization, but subsequent calls to onResume() effectively scrolls it to the top.
Any ideas on why this is happening? Tips on how to further investigate are also very welcome.
Probably too late to help you Pedro, but I did figure out a solution that worked for me. I noticed in the TabHost source that it requests Focus if nothing else in the view has focus. So I made sure that the first component in the Scroll view was focusable and requested focus for it.
// Get the first component and make sure it is focusable. Note you have to use setFocusableInTouchMode and not setFocusable for this to work.
TextView v = (TextView) view.findViewById(R.id.first_component_in_view);
v.setFocusableInTouchMode(true);
v.requestFocus();
Hopefully this will help someone.
I have a similar problem and haven't solved it. However, to force the scroll view to scroll you need to update it's progress through a separate thread to post on the UI. e.g.
quickScroll.post(new Runnable() {
#Override
public void run() {
quickScroll.scrollTo(0, 54);
}
});
Where quickScroll is my ScrollView. I believe the reason for this is that you have diagrammatically defined your layout, or inflated it from a custom viewgroup. So setContentView doesn't hold a reference to the scrollView, so you have to force the UI to update off the variable allocated to the ScrollView.
I'm actually making an application using a ViewFlipper to display 3 differents custom views. These views are, for each one, in a ScrollView (putting the ViewFlipper in a single ScrollView isn't making my onFling gestures really efficient). And i'm actually trying to synchronize the three Scrollbars position. At this point using a single ScrollView would have been easier but i trying to not use this solution.
I'm using a ScrollListener for each ScrollView to set the others ScrollView scroll position like in this thread : Synchronise ScrollView scroll positions - android.
The problem is that this method will works but the first time. When i'm on a ScrollView, the two others are not drawn already and their height is null. So setting their scroll position isn't working.
/*Instanciate ScollViews*/
scrollViewLeft = (ScrollableScrollView) findViewById(R.id.scroll_prev);
scrollViewCenter =(ScrollableScrollView) findViewById(R.id.scroll_current);
scrollViewRight = (ScrollableScrollView) findViewById(R.id.scroll_next);
/*Fill views with events*/
setDayEvents(calIns.getPrevCal(),leftDayView);
setDayEvents(calIns.getActualCal(),centerDayView);
setDayEvents(calIns.getNextCal(),rightDayView);
scrollViewLeft.setScrollViewListener(this);
scrollViewCenter.setScrollViewListener(this);
scrollViewRight.setScrollViewListener(this);
So finally my question is, should i use another method to find the scroll position of the two others views ? Or can i force the view to be drawn in a ViewFlipper ?
Thanks :)
Clement.
Try saving the positions in global variables before you change the views and then just apply it to your current view.
Thanks Egor for having answered. I think it is a good idea. I already have a Singleton to handle variables. So i add the variable position with getters and setters.
The probleme here is than even if I override the method "onSizeChanged" in my custom ScrollView, this will apply only for the first view shown.
PS: I'm still starting on Android development and I don't really know how to handle with
interactions on views when they aren't ready (for example getting width when they are not drawn yet).
Edit 1 day later :
Ok i found what was the problem, it is because the size of my views inside the scrollviews wasn't defined (the scrollview was drawn before the view inside it). So by setting the scroll inside the "onSizeChanged" method of the views works well.
I have a ScrollView that contains an number of other views (TextViews, ImageViews, etc.). The ScrollView is taller than the screen. I have an AsyncTask that updates the children of the ScrollView based on an http response.
I've discovered an interesting behavior that I can't figure out how to work around. If I set any of the children's visibilities to View.INVISIBLE as part of the AsyncTask.onPostExecute(), everything works fine.
However, if I set any of the children's visibilities to View.GONE, the ScrollView jumps down from the top when onPostExecute() is called. Exactly how far seems to vary. I'm guessing that re-laying out the ScrollView is causing it to scroll away from the top for some reason.
So the question is: is there a way to either prevent or work around this behavior?
PS. Using ScrollView.jump(FOCUS_UP) as a workaround isn't ideal since that'll force the user to the top even if they had intended to scroll down.
EDIT: Actually, I was wrong. The problem wasn't with a child view being marked gone, the problem was with a sibling view being marked gone and the ScrollView getting resized. My ScrollView is inside a LinearLayout that also contains a Button. When the button is set to GONE, the ScrollView gets resized to take up the available space, causing it to scroll away from the top. Different cause, still looking for a workaround though if possible.
I had the same problem. If layout jumping add tha following in ur top lever layout
android:descendantFocusability="blocksDescendants"
Example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >