实现里面有可滑动的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>