Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollingViewBehavior for ListView

I have two activities using AppBarLayout with a Toolbar and TabLayout from support library 22.

The layout of both is pretty similar: A Toolbar at the top, below it TabLayout, below it a ViewPager containing 3 Fragments.

The first activity's Fragment has a RecyclerView, the second activity's Fragment is using a ListView instead.

The scrollable Toolbar example from https://github.com/chrisbanes/cheesesquare is working fine on the first activity using the RecyclerView, but on with the ListView.

I've tried created a custom ListViewScrollBehavior that extends AppBarLayout.ScrollingViewBehavior, but so far no luck. The TouchEvents are passed to the custom class only for horizontal scrolling, but not when scrolling the ListView (vertically).

Any way to use a CoordinatorLayout with ListView?

like image 565
marmor Avatar asked Jun 03 '15 06:06

marmor


2 Answers

The only solution to make it work now is to use this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {      listView.setNestedScrollingEnabled(true); } 

It will obviously only work on Lollipop.

like image 62
Nicolas POMEPUY Avatar answered Sep 25 '22 00:09

Nicolas POMEPUY


Alternative solution to Nicolas POMEPUY's answer is to use ViewCompat.setNestedScrollingEnabled(View, boolean)

ViewCompat.setNestedScrollingEnabled(listView, true); 

Of course nested scrolling behavior will only work from Lollipop.

like image 20
hidro Avatar answered Sep 22 '22 00:09

hidro