步骤:
1.将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内
2.将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll
3.给NestedScroolView设置app:layout_behavior="@String/appbar_scrolling_view_behavior"就大功告成了(根布局一定要是CoordinatorLayout)
xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="220dp"
app:contentScrim="#000000"
app:layout_scrollFlags="scroll">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:gravity="center"
android:text="banner"
android:textColor="#ffffff" />
</android.support.design.widget.CollapsingToolbarLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="suspension part" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
java代码
public class ThirdActivity extends AppCompatActivity {
private ListView lv;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
lv = (ListView) findViewById(R.id.lv);
list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add("第" + (i + 1) + "项数据!");
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}
网友评论