Android-ParallaxHeaderViewPager icon indicating copy to clipboard operation
Android-ParallaxHeaderViewPager copied to clipboard

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

Open agnel opened this issue 11 years ago • 14 comments

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 avatar Jul 04 '14 13:07 agnel

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

agnel avatar Jul 08 '14 16:07 agnel

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

Eeram avatar Sep 17 '14 16:09 Eeram

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.

Eeram avatar Sep 18 '14 13:09 Eeram

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

yshrsmz avatar Oct 09 '14 07:10 yshrsmz

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

Eeram avatar Oct 09 '14 09:10 Eeram

Did anyone succeed doing that example that @yshrsmz gave?

@Vieuma

leonardossantos avatar Oct 31 '14 03:10 leonardossantos

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

Eeram avatar Oct 31 '14 08:10 Eeram

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

leonardossantos avatar Oct 31 '14 18:10 leonardossantos

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

agnel avatar Dec 07 '14 06:12 agnel

@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 avatar Dec 17 '14 14:12 eirini91

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

Eeram avatar Dec 17 '14 14:12 Eeram

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

eirini91 avatar Dec 17 '14 15:12 eirini91

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

Adnan9011 avatar Jun 19 '15 10:06 Adnan9011

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

Eeram avatar Jun 19 '15 10:06 Eeram