ksoichiro / Android-ObservableScrollView

Android library to observe scroll events on scrollable views.
http://ksoichiro.github.io/Android-ObservableScrollView/
Apache License 2.0
9.65k stars 2.06k forks source link

Loading fragments from Main Activity #168

Open laminsk opened 9 years ago

laminsk commented 9 years ago

Hi there

Thanks for the wonderful library :) I'm trying to use this for my project and currently experiencing a problem which I hope you could be able to help me out please.

I have main activity which I use to load all other fragments. The main activity is using this library https://github.com/umano/AndroidSlidingUpPanel. Both android.support.v7.widget.Toolbar and container body (which I uses to load child fragments) is in MainActivity layout.

I'm trying to use this example ParallaxToolbarScrollViewActivity and trying to achieve the same result using fragment. I would be most grateful if you could provide a pointer or any suggestion would be greatly appreciated.

Leo

eneim commented 9 years ago

Implement ObservableScrollViewCallbacks on your MainActivity

In your fragment which includes an ObservableScrollView, implement the following flows:

  1. You need a member ObservableScrollViewCallbacks mCallback or something like that, in onAttach(), get the ObservableScrollViewCallbacks from your Activity:
mCallback = (ObservableScrollViewCallbacks) activity;
  1. in onViewCreated or onCreateView, after you setup your ScrollView, set that scrollview's ObservableScrollViewCallbacks by your mCallback.
mScrollView.setScrollViewCallbacks(mCallback);

Please tell me if it helps.

laminsk commented 9 years ago

Thanks for the reply. Unfortunately, I still can't get it to work yet.

Currently, onScrollChanged implementation is in fragment but since I remove "ObservableScrollViewCallbacks" from fragment and put that in activity, it no longer works.

Since I implemented ObservableScrollViewCallbacks in Activity, how the activity would interact with components from Fragment, for instance:

public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
    int baseColor = getResources().getColor(R.color.primary);
    float alpha = Math.min(1, (float) scrollY / mParallaxImageHeight);
    mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor));
    ViewHelper.setTranslationY(mImageView, scrollY / 2);
   // please note that mImageView in fragment, not in activity
}

Thanks again for helping me out. Leo

eneim commented 9 years ago

@laminsk I create an example on Github:

https://github.com/eneim/ScrollSample

See if it helps.

laminsk commented 9 years ago

Thank you so much again for your help. There's one tiny issue with top Actionbar, it's still visible when you click on sliding up panel. When sliding up, the actionbar should be hidden behind the panel.

What I'm trying to achieve is just like "Google Play" Music app, when you click on a particular album in Google play music app, it will show the album artwork with transparent action bar initially. However, when scrolling up, actionbar with be visible again along with slide in animation, it will then slide out when scrolling down.

Many thanks once again for your kind help.

eneim commented 9 years ago

@laminsk Since I put that toolbar to the last inside MainActivity's FrameLayout xml so It will be the view on top therefore It's always shown up. You can just put it before your SlidingUpLayoutPanel inside that xml file to achieve what you want.

laminsk commented 9 years ago

@eneim Thank you once again and much appreciated for your help.

eneim commented 9 years ago

@laminsk you are welcome. Please checkout this, I fix it for your requirement.

Change MainActivity layout to overlay SlidingPanel over Toolbar

laminsk commented 9 years ago

@eneim That's exactly how I wanted it. You are a star!

eneim commented 9 years ago

@laminsk you are welcome. Is this issue Ok to close? ^^

laminsk commented 9 years ago

Yes, please go ahead. Its all sorted :)

On Thu, Jul 16, 2015 at 3:30 AM, Nam Nguyen Hoai notifications@github.com wrote:

@laminsk https://github.com/laminsk you are welcome. Is this issue Ok to close? ^^

— Reply to this email directly or view it on GitHub https://github.com/ksoichiro/Android-ObservableScrollView/issues/168#issuecomment-121805695 .