Issue scrolling the list/recyclview down
ayz4sci opened this issue · comments
Whenever I scroll down, the header scrolls and becomes visible; but I have to release and scroll down again for the scrolling to continue.
How do I change this behavior such that with one scroll down/drag down the header will show while the list will still continue to scroll down.
Thanks so much.
Please how do I reduce the slidedown speed of the header? I want it to slide down with the same speed as the list fling speed.
Set scroll friction via code scrollableLayout.setFriction(float friction)
or via xml attribute app:scrollable_friction="0.075"
.
Try playing with values in the sample application, it has seek bar with current scroll friction indication. For my own taste something between 0.05 & 0.075 feels smooth
Thanks.
@noties somehow recyclerView is always null in canScrollVertically
method in my Fragment. Do you have any idea why this might be the case?
@squeeish Well, it might be two things: first check if you have RecyclerView in your layout file, second check the id of the RecyclerView (most likely you miss typed it)
@noties it's definitely in my layout file and the id is correct as I'm setting layout managers and adapters to it. I've 4 tabs in a ViewPager
with Android Design Support Library's TabLayout
as the tab headers. The header is a RelativeLayout
.
When I debug, I can see that the CanScrollVertically
chain is there (delegate, adapter, fragment) but in all 4 fragments' CanScrollVertically
method, the recyclerview variable is always null.
Ok I've solved it. Turns out it was my FragmentPagerAdapter returning new instances of the fragments in getItem
. 😄
@noties Could you help me out?
- Go to developer options and check "Do not keep activities".
- Start the sample app and click "Inside Fragment".
- Scroll up and down. The header only shows when the top of the list is reached.
- Press the home button, then open the sample app again.
- Scroll up and down.
CanScrollVerticallyDelegate
seems to always return false this time.
Do you have any idea why this might be the case? This behaviour is not seen in MainActivity
. If you are in debug mode, canScrollVertically
method in RecyclerViewFragment
thinks that mRecyclerView
is null, which isn't supposed to be the case.
EDIT: Fixed. mRecyclerView
has to be static. Same for mListView
in ListViewFragment
.
Hi! Nice catch!
It happens because we pass to the ViewPagerAdapter
newly created fragments, but the previous ones are still attached to the FragmentManager
. Thus, our newly created fragments won't be added, won't o through onCreateView()
and that's why they will always return false
from canScrollVertically
method. So, to work around this issue, we must do something like this:
private List<BaseFragment> fragments() {
final FragmentManager manager = getChildFragmentManager();
final BaseFragment list;
{
final Fragment fragment = manager.findFragmentByTag(ListViewFragment.TAG);
if (fragment == null) {
list = ListViewFragment.newInstance(0x80FF0000);
} else {
list = (ListViewFragment) fragment;
}
}
final BaseFragment recycler;
{
final Fragment fragment = manager.findFragmentByTag(RecyclerViewFragment.TAG);
if (fragment == null) {
recycler = RecyclerViewFragment.newInstance(0x8000FF00);
} else {
recycler = (RecyclerViewFragment) fragment;
}
}
return Arrays.asList(list, recycler);
}
And then:
final ViewPagerAdapter adapter = new ViewPagerAdapter(
getChildFragmentManager(),
getResources(),
fragments()
);
This solution might work if you have not much fragments. Otherwise it's better to create some kind of helper.
I will update sample
with the next library release