关于CoordinatorLayout的一些使用经验

实现里面有可滑动的ListView或者其他的能引起冲突的控件时整体联动的效果。

本来可以通过ScrollView嵌套ListView来实现:

ScrollView嵌套ListView来实现首先遇到的问题是ListView不能显示出应有的高度,只能显示一项,哪怕你给他留的空间还很富足,这就是显示不全的问题,而且你滑动ListView并不能实现整体联动的效果,然后查阅资料发现重写ListView的onMeasure方法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}
此时ListView会显示出你设置的大小

此时已经能够实现整体联动了,然而紧接着第二个问题是,一开始显示的时候它会显示ListView的底部,查阅资料得知,在代码中动态设置ListView的setFocusable属性为false即可在初始显示时显示到顶部(在xml中设置无效),或者外部的ScrollView调用smoothScrollTo解决。

以上见解均采自点击打开链接

这样一来,就可以实现效果了,但是在这里点击打开链接看到大神解析说这样做会很消耗内存,所以就采用了CoordinatorLayout来实现:

首先,CoordinatorLayout可以直接作为一个布局文件中最外层的父容器来使用,也可以作为一个子容器来使用;

然后,CoordinatorLayout的第一个直接子View必须是AppBarLayout,然后他的第一个子View必须有

app:layout_scrollFlags="scroll|enterAlways"

这个属性,如果第一个没有则不会滑动,那我如果在布局最上面有一个搜索框怎么办,我不想让他移出屏幕?那就把他放在CoordinatorLayout外面来实现;

然后,下面的ListView或者RecyclerView添加这个属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"
如果是ListView,你会发现,这样一来下面的ListView不能滑动了,所以

//这一句至关重要,如果没有这一句使用CoordinatorLayout会无法滑动ListView!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    mAllHots.setNestedScrollingEnabled(true);
}
到此,OK!如果是RecyclerView则不需要添加最后这局代码。

AppBarLayout或者ToolBar是带阴影的,想要取消阴影需要添加属性:

app:elevation="0dp"

最后,附xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backGray"
    android:orientation="vertical"
    >
    <!--搜索框-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/main"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:src="@drawable/title_img"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_weight="2"
            android:background="@color/backGray"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:padding="6dp"
                android:src="@drawable/search"/>

            <EditText
                android:id="@+id/inputSearch"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/backGray"
                android:hint="昌吉旅游"
                android:singleLine="true"/>

        </LinearLayout>

    </LinearLayout>


    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                     xmlns:app="http://schemas.android.com/apk/res-auto"
                                                     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"
            android:background="@color/backGray"
            style="@style/MyToolBar"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll|enterAlways"
                >

                <android.support.v4.view.ViewPager
                    android:id="@+id/pagerTop"
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    ></android.support.v4.view.ViewPager>

                <android.support.v4.view.ViewPager
                    android:id="@+id/pagerBottom"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:layout_marginBottom="10dp"
                    android:background="@color/main"
                    ></android.support.v4.view.ViewPager>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/main"
                android:orientation="vertical">

                <com.hardy.person.changji.utils.others.DrawableTextView
                    android:layout_width="wrap_content"
                    android:layout_height="45dp"
                    android:drawablePadding="10dp"
                    android:gravity="center"
                    android:text="热门推荐"
                    android:textColor="@color/text_selected_true"
                    android:textSize="18sp"
                    app:drawableHeight="35dp"
                    app:drawableSrc="@drawable/button"
                    app:drawableWidth="5dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0.5dp"
                    android:background="@color/divider"/>

            </LinearLayout>
        </android.support.design.widget.AppBarLayout>

        <!--ScrollView嵌套ListView实现联动时用到-->
        <!--<com.hardy.person.changji.views.MyListView
            android:id="@+id/allHots"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/main"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
        </com.hardy.person.changji.views.MyListView>-->

        <ListView
            android:id="@+id/allHots"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/main"
            android:entries="@array/test"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
        </ListView>

    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CoordinatorLayout 是 Android Design Support Library 提供的一个特殊的布局容器,它可以协调其内部的子 View 之间的交互行为,实现各种复杂的交互效果,比如 AppBarLayout 和 FloatingActionButton 的联动效果。 CoordinatorLayout 的主要作用是让子 View 之间可以通过 Behavior 进行交互。Behavior 是指子 View 在 CoordinatorLayout 中的交互行为的定义,可以让子 View 之间实现联动效果,如 FloatingActionButton 随着 Snackbar 的出现和消失而改变位置,子 View 之间的交互行为通过 Behavior 实现。 使用 CoordinatorLayout 需要注意以下几点: 1. CoordinatorLayout 必须作为根布局。 2. 子 View 需要设置 app:layout_behavior 属性,指定其交互行为的 Behavior。 3. 子 View 的交互行为需要在 Behavior 中定义。 下面是一个简单的 CoordinatorLayout 的示例: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="200dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/image"/> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"/> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 在上面的示例中,AppBarLayout 和 CollapsingToolbarLayout 实现了一个可折叠的 Toolbar,RecyclerView 使用了 appbar_scrolling_view_behavior Behavior,实现了和可折叠的 Toolbar 的联动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值