起因是这样的,拿到一个产品需求,ui如下,具体交互细节呢,美工小姐姐在设计稿上标了一小行字:去看看每日优鲜。既然如此,那就先看看别人家是怎么做的吧。
1.工具篇:
先参考文章:查看布局
- 先找到android sdk 的目录-》tools -》monitor.bat. 启动。(我的是在此目录下,你的不一定哦-)
- 按照如下操作,之后就获取到别人app的布局视图了
- 选取其中某个交互开始分析:
打开app,在此分类界面,一只手不停下拉,不松手,另外一只手按照上面的步骤进行操作,获取到的截图如下:
看看红框我们能分析出哪些信息:
① 布局是ViewGroup,可以下拉刷新,上拉加载更多,结合resource-id:srl-produce_classify,是不是有点熟悉了,大胆猜测,极有可能用的是SmartRefreshLayout,因为它正好是ViewGroup。这里的header需要自定义了。官网也告诉了如何自定义,参考即可。
②红色部分往上滑的时候,图片区域是要被滑出屏幕的,然后是综合,销量的布局置顶不再滑动,之后才是下面recycleview的滑动,结合右边来看,最外层用的是NestedScrollView(根据id来的),第一个子view是LinearLayout,第二个子view是viewgroup,因为要处理滑动事件,我们熟悉的CoordinatorLayout就是一个ViewGroup,它可以帮我们处理滑动事件,那么不妨猜测第二个子view用的就是此布局了。
2. 实践篇
貌似我们已经分析得差不多了,那么开始实践上手验证下我们的猜想:
- 布局方式:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ffffff"
>
<TextView
android:id="@+id/tvSearch"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="搜索"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvLeft"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintVertical_weight="1"
app:layout_constraintTop_toBottomOf="@+id/tvSearch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/clRight"
app:layout_constraintBottom_toBottomOf="parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
tools:listitem="@layout/item_test"
/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/clRight"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintVertical_weight="1"
app:layout_constraintTop_toBottomOf="@+id/tvSearch"
app:layout_constraintLeft_toRightOf="@+id/rvLeft"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>