wwa*_*ang 43 android android-toolbar android-design-library android-coordinatorlayout
我正在尝试使用最新的设计库让我的工具栏在滚动时隐藏/显示.我的问题是我在片段中的滚动内容,我只是将它注入到FrameLayout容器中,它不起作用.这是我的活动:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/layout_toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/navigation_drawer_container"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_nav_drawer_anon" />
Run Code Online (Sandbox Code Playgroud)
和我的片段:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp"
android:fontFamily="sans-serif"
android:color="@color/dark_grey"
android:padding="60dp"/>
Run Code Online (Sandbox Code Playgroud)
和工具栏:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
style="@style/Widget.MyApp.ActionBar" />
Run Code Online (Sandbox Code Playgroud)
小智 21
用.替换你的FrameLayout android.support.v4.widget.NestedScrollView
NestedScrollView就像ScrollView一样,但它支持在新旧版本的Android上充当嵌套滚动父级和子级.默认情况下启用嵌套滚动.
小智 12
这种行为的原因是Framelayout没有指定行为.CoordinatorLayout依赖于子视图来处理行为.
你可以在这里阅读
http://android-developers.blogspot.in/2015/05/android-design-support-library.html
它说明了这一点
CoordinatorLayout和自定义视图
需要注意的一件事是CoordinatorLayout对FloatingActionButton或AppBarLayout工作没有任何天生的理解 - 它只是以Coordinator.Behavior的形式提供了一个额外的API,它允许子视图更好地控制触摸事件和手势以及声明彼此之间的依赖关系并通过onDependentViewChanged()接收回调.
视图可以使用CoordinatorLayout.DefaultBehavior(YourView.Behavior.class)注释声明默认行为,或使用app:layout_behavior ="com.example.app.YourView $ Behavior"属性在布局文件中设置它.该框架使任何视图都可以与CoordinatorLayout集成.
编辑:虽然FrameLayout不是自定义视图,但它不指定CoordinateLayout搜索的行为.
Ema*_*ger 11
使用FrameLayout作为CoordinatorLayout的子代可以很好地工作.工具栏正在崩溃,就像它应该的那样.当我使用过时的库时,我在开始时遇到了问题.
以下是我正在使用的gradle依赖项:
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)
我正在使用FrameLayout
属性app:layout_behavior="@string/appbar_scrolling_view_behavior"
作为CoordinatorLayout
活动布局中的子项.将FrameLayout
用作片段的容器.我的片段布局的根元素是a RecyclerView
或a NestedScrollView
.
以下是活动的布局:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/..."
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.AppBarLayout
android:layout_height="192dp"
android:layout_width="match_parent"
>
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:id="@+id/.."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/..."
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_sessions"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
我的第一个片段的布局如下所示:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="..."
/>
Run Code Online (Sandbox Code Playgroud)
我的第二个片段的布局如下所示:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="..."
>
...
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)