Open agnel opened 10 years ago
Its not working for ScrollViews at all. I guess for scrollviews the code for adjustScroll and onScroll has to be changed?
Hi agnel, I'd also be very interested in using this with ScrollViews! Have you find any solution to this problem?
So, I found a solution : it's working, but it's probably not the best solution in terms of performance and it may be buggy (didn't notice any bug yet though).
Just in case somebody would look for this answer, here are the steps to use ScrollViews in the ViewPager :
void onScroll(ScrollView view, int x, int y, int oldX, int oldY, int pagePosition);
Also, in the ScrollTabHolderFragment abstract class, implement it and keep it empty (just like the other onScroll(...)
method.)adjustScroll(int scrollHeight)
method to : adjustScroll(int scrollHeight, int headerTranslationY)
. (*)NotifyingScrollView.OnScrollChangedListener
instead of AbsListView.OnScrollListener
adjustScroll(...)
and onScrollChanged(...)
like this : @Override
public void adjustScroll(int scrollHeight, int headerTranslationY)
{
scrollView.setScrollY(headerTranslationY - scrollHeight);
}
@Override
public void onScrollChanged(ScrollView view, int l, int t, int oldl, int oldt)
{
if (mScrollTabHolder != null)
mScrollTabHolder.onScroll(view, l, t, oldl, oldt, pagePosition);
}
onScroll
method in MainActivity. Mine looks like this :
@Override
public void onScroll(ScrollView view, int x, int y, int oldX, int oldY, int pagePosition)
{
if (viewPager.getCurrentItem() == pagePosition)
{
foodtruckHeader.setTranslationY(Math.max(-view.getScrollY(), minHeaderTranslation));
float ratio = clamp(foodtruckHeader.getTranslationY() / minHeaderTranslation, 0.0f, 1.0f);
setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
}
}
And this is it! Again, I'm sure this is not the best solution, but... it's working for me. If anyone has a better solution, you're welcome to change this!
(*) : I've done this because the methods to set the scroll position are different in ListViews and ScrollViews. In the adjustScroll(...)
method, we use scrollView.setScrollY(...)
instead of listView.setSelectionFromTop(...)
, and it requires the absolute position to set the ScrollView.
Never been able to find such a post, despite the long hours spent searching... Thanks @yshrsmz !
Did anyone succeed doing that example that @yshrsmz gave?
@Vieuma
@AndroidRio sorry, didn't try it since I found a way by myself! Have you tried the solution I posted above?
@Vieuma Not yet but I will try it today for sure :+1:
Thanks @Vieuma. I was out of town for training purpose. I'll definitely try your solution for sure and let you know.
@Vieuma I tried your solution and it works without any bugs. I have an issue with the adjustScroll method. What are you passing to it? I tried a few things but it doesn't seem correct.
@eirini91 The only time adjustScroll is called is in the ViewPager.OnPageChangeListener.onPageSelected(int page) method (you can find it in the MainActivity.java on this repo).
Here's how I changed it :
currentHolder.adjustScroll((int) (headerView.getHeight() + headerView.getTranslationY()), headerView.getHeight());
@Vieuma Thanks a lot!! everything works fine now! I was sending the height and the translation as the parameters and I had random "jumps" at the top of the scrollview! Thanks for the fast respond :)
@Vieuma Can u explain me , what's foodtruckHeader.setTranslationY(...)
@Adnan9011 I was working on a foodtrucks finder app, I just forgot to replace the var name : foodtruckHeader is actually just the header ViewGroup. The call to setTranslationY(...) allows to move the collapse/expand the header while the user is scrolling.
The ViewPager in my app contains layouts with ListViews, ExpandableListViews, ScrollView, RelativeLayout and LinearLayout(with a MapFragment in this). Everything works fine for Tabs containing ListViews and ExpandableListViews! But.. not for the other layouts? How to proceed now?
Please Help me I'm trying to build an app for our college Festival.
Thanks!