kmshack / Android-ParallaxHeaderViewPager

DEPRECATED
Apache License 2.0
1.39k stars 336 forks source link

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

Open agnel opened 10 years ago

agnel commented 10 years ago

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 commented 10 years ago

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

Eeram commented 9 years ago

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

Eeram commented 9 years ago

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 commented 9 years ago

FYI http://nerds.airbnb.com/host-experience-android/

Eeram commented 9 years ago

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

leonardossantos commented 9 years ago

Did anyone succeed doing that example that @yshrsmz gave?

@Vieuma

Eeram commented 9 years ago

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

leonardossantos commented 9 years ago

@Vieuma Not yet but I will try it today for sure :+1:

agnel commented 9 years ago

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

eirini91 commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

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

Eeram commented 9 years ago

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