CoordinatorLayout not working
Solution 1
I believe you need to make your ListView
implement both ScrollingView
and NestedScrollingChild
interfaces.
It's not the easiest thing, but you should be able to do it if you look at RecyclerView
's source code. It makes use of a NestedScrollingChildHelper
and you should be able to do the same.
Solution 2
Currenlty not all views have the expected behavior with the CoordinatorLayout
.
Your views should implement the NestedScrollView
interface and have to handle the nested scroll events.
The RecyclerView
and the NestedScrollView
(version 22) support this behavior.
However you can use also the AbsListView
(ListView
and GridView
) but only with API21+.
Just add something like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
Related videos on Youtube
dnlbaines
Updated on October 25, 2022Comments
-
dnlbaines over 1 year
I am attempting to implement a
CoordinatorLayout
from the newly announced Android Design Support Library and I have used the following code in my XML layout as per the sample here:<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways"/> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
However the action bar does not hide when I scroll down the view. I can't figure out why this is not working.
Edit: As far as I am aware it appears that
CoordinatorLayout
does not work withListView/GridView/ScrollViews
and only works withRecyclerView
andNestedScrollView
. Unfortunately simply switching to one of these views is not a possibility for me so I am still looking for a solution. -
dnlbaines almost 9 yearsI'm using several views within the ViewPager, one of which is a ListView. I did try to use just a ListView on its own with no difference.
-
aows almost 9 yearsI'd give it a try with a
RecyclerView
, and using the latest support library:compile 'com.android.support:recyclerview-v7:22.2.0'
. The fragment should have just the recyclerview, do not wrap it into any other layout. -
dnlbaines almost 9 yearsMy app is loading a list of articles into a ListView but uses two other views, one to show a progress spinner and another to show some text when no articles are loaded - how will I be able to do this with just a RecyclerView?
-
aows almost 9 yearsThe view needs to be scrollable, otherwise you won't get the effect you want. You can try to wrap all of your views within a
NestedScrollView
, but it's not recommended to use something like a list inside another scrollable view, might have weird secondary problems. -
aows almost 9 yearsI haven't looked at the source code, but my guess here is that the
CoordinatorLayout
is waiting for a "scroll event" in order to collapse the toolbar (that's why you need to addapp:layout_behavior="@string/appbar_scrolling_view_behavior"
). If you use a layout, that event will never be triggered. Again, this is just a guess. -
dnlbaines almost 9 yearsYeah a NestedScrollView works but causes lots of other issues, I will try again and see if I can get ListView to work on its own as that would be more desirable. (Edit - Still can't get ListView to work for some reason)
-
aows almost 9 yearsI'm actually having the same issue. I want to show more views than just a
RecyclerView
. My approach so far was to use different view types within the list. In your case, the adapter should return the size of the items if any, or 1 if it is empty. Then you can inflate a different view and do whatever you want there. Makes things more complicated but it is what it is right now. -
dnlbaines almost 9 yearsThe only layouts that seem to work with it are RecylerView and NestedScrollView, ListView and ScrollView do not work. This basically renders this functionality useless to me unless there is a workaround.