Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use this for fragments containing layouts other than ListViews? #5

Open
agnel opened this issue Jul 4, 2014 · 14 comments
Open

Comments

@agnel
Copy link

agnel commented Jul 4, 2014

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!

@agnel
Copy link
Author

agnel commented Jul 8, 2014

Its not working for ScrollViews at all.
I guess for scrollviews the code for adjustScroll and onScroll has to be changed?

@Eeram
Copy link

Eeram commented Sep 17, 2014

Hi agnel, I'd also be very interested in using this with ScrollViews! Have you find any solution to this problem?

@Eeram
Copy link

Eeram commented Sep 18, 2014

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 :

  1. Create a custom ScrollView that allows us to register a listener on the scroll events. I used the NotifyingScrollView explained by Cyrril Mottier here http://cyrilmottier.com/2013/05/24/pushing-the-actionbar-to-the-next-level/
  2. Create a layout with a NotifyingScrollView, a LinearLayout (or Relative) in the NotifyingScrollView, and add the dummy header view to the top of the LinearLayout.
  3. Add the following method to the ScrollTabHolder interface : 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.)
  4. Change the adjustScroll(int scrollHeight) method to : adjustScroll(int scrollHeight, int headerTranslationY). (*)
  5. Apply the following changes to the SampleListFragment :
    • make it implement NotifyingScrollView.OnScrollChangedListener instead of AbsListView.OnScrollListener
    • implement the 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);

    }
  1. Finally, implement the new 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.

@yshrsmz
Copy link

yshrsmz commented Oct 9, 2014

@Eeram
Copy link

Eeram commented Oct 9, 2014

Never been able to find such a post, despite the long hours spent searching... Thanks @yshrsmz !

@leonardossantos
Copy link

Did anyone succeed doing that example that @yshrsmz gave?

@Vieuma

@Eeram
Copy link

Eeram commented Oct 31, 2014

@androidrio sorry, didn't try it since I found a way by myself! Have you tried the solution I posted above?

@leonardossantos
Copy link

@Vieuma Not yet but I will try it today for sure 👍

@agnel
Copy link
Author

agnel commented Dec 7, 2014

Thanks @Vieuma. I was out of town for training purpose. I'll definitely try your solution for sure and let you know.

@eirini91
Copy link

@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.

@Eeram
Copy link

Eeram commented Dec 17, 2014

@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());

@eirini91
Copy link

@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 :)

@Adnan9011
Copy link

@Vieuma Can u explain me , what's foodtruckHeader.setTranslationY(...)

@Eeram
Copy link

Eeram commented Jun 19, 2015

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants