CoordinatorLayout not working

ぃ、小莉子 提交于 2019-12-06 20:36:02

问题


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 with ListView/GridView/ScrollViews and only works with RecyclerView and NestedScrollView. Unfortunately simply switching to one of these views is not a possibility for me so I am still looking for a 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.




回答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);
}



回答3:


It depends on what you are showing in your ViewPager. If you use a list / recyclerview, it should scroll correctly.




回答4:


Views can declare a default Behavior by using the CoordinatorLayout.DefaultBehavior(YourView.Behavior.class) annotation,or set it in your layout files by with the app:layout_behavior="com.example.app.YourView$Behavior" attribute. This framework makes it possible for any view to integrate with CoordinatorLayout.

So I think solution here is override the AppBarLayout.Behavior class




回答5:


This is work for me.

It is code for GridView. But you can extend ListView instead of GridView.

https://gist.github.com/sakurabird/6868765



来源:https://stackoverflow.com/questions/30561037/coordinatorlayout-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!