当列表滑动时,图片逐渐消失,toolBar悬停在头部。
<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns: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"android:fitsSystemWindows="true"><com.google.android.material.appbar.AppBarLayoutandroid:id="@+id/app_bar"android:layout_width="match_parent"android:layout_height="wrap_content"android:fitsSystemWindows="true"><com.google.android.material.appbar.CollapsingToolbarLayoutandroid:id="@+id/toolbar_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:fitsSystemWindows="true"app:layout_scrollFlags="scroll|exitUntilCollapsed"app:statusBarScrim="@android:color/transparent"><ImageViewandroid:id="@+id/sortbanner"android:layout_width="match_parent"android:layout_height="80dp"android:scaleType="fitXY"android:src="@drawable/back_vip" /></com.google.android.material.appbar.CollapsingToolbarLayout><LinearLayoutandroid:id="@+id/llsort"android:layout_width="match_parent"android:layout_height="40dp"android:background="@color/purple_200"android:gravity="center_vertical"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:gravity="center"android:text=""android:textColor="#5A5A5A" /><TextViewandroid:id="@+id/tv"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="综合"android:textColor="#5A5A5A" /><TextViewandroid:id="@+id/tv1"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="销量"android:textColor="#5A5A5A" /><RelativeLayoutandroid:id="@+id/rl"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1.2"android:gravity="right"android:paddingRight="20dp"><TextViewandroid:id="@+id/tv3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toLeftOf="@+id/ft_cb"android:text="价格"android:textColor="#5A5A5A" /><ImageViewandroid:id="@+id/ft_cb"android:layout_width="wrap_content"android:layout_height="16dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:button="@null"android:src="@drawable/sortdown" /></RelativeLayout></LinearLayout></com.google.android.material.appbar.AppBarLayout><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/goodsRecyclerView"android:layout_width="match_parent"android:layout_height="match_parent" /></androidx.core.widget.NestedScrollView></androidx.coordinatorlayout.widget.CoordinatorLayout>
public class MainActivity extends AppCompatActivity {RecyclerView recyclerView;private List<String> list;private NormalAdapter normalAdapter;private LinearLayoutManager linearLayoutManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);recyclerView=findViewById(R.id.goodsRecyclerView);initList();initView();}private void initList() {list = new ArrayList<>();for (int i = 1; i <= 100; i++) {list.add(i + "");}}private void initView() {linearLayoutManager = new LinearLayoutManager(this);normalAdapter = new NormalAdapter(list);recyclerView.setLayoutManager(linearLayoutManager);recyclerView.setNestedScrollingEnabled(false);recyclerView.setAdapter(normalAdapter);}
}
demo地址