当列表滑动时,图片逐渐消失,toolBar悬停在头部。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android: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">
<ImageView
android: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>
<LinearLayout
android:id="@+id/llsort"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/purple_200"
android:gravity="center_vertical">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text=""
android:textColor="#5A5A5A" />
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="综合"
android:textColor="#5A5A5A" />
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="销量"
android:textColor="#5A5A5A" />
<RelativeLayout
android:id="@+id/rl"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:gravity="right"
android:paddingRight="20dp">
<TextView
android: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" />
<ImageView
android: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.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android: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;
@Override
protected 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地址