这次实现标题栏随着上滑下滑显示隐藏
实现这个效果需要,Support Design库中的CoordinatorLayout和AppBarLayout进行配合才行。
dependencies {
...
implementation 'com.android.support:design:27.1.1' //必须添加
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
}
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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
app:title="我是标题"
app:titleTextColor="#fff"
android:id="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways" //这个属性实现随着页面滚动标题栏(toolbar)显示隐藏
android:background="@color/colorPrimary"
app:navigationIcon="@drawable/ic_back"
android:layout_height="50dp">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycle"
app:layout_behavior="@string/appbar_scrolling_view_behavior"//这个属性只有在布局是CoordinatorLayout 时才有,是让让控件在AppBarLayout之下的
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Activity代码
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
RecyclerView recyclerView;
private BaseQuickAdapter<String, BaseViewHolder> baseQuickAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
recyclerView=findViewById(R.id.recycle);
setSupportActionBar(toolbar);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final ArrayList<String> strings = new ArrayList<>();
for(int x=0;x<10;x++){
strings.add("我是条目"+x);
}
baseQuickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item, strings) {
@Override
protected void convert(BaseViewHolder helper, String item) {
helper.setText(R.id.tv_title, item);
}
};
baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
// baseQuickAdapter.isFirstOnly(false);
baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
}
});
recyclerView.setAdapter(baseQuickAdapter);
}
}
还需要在主题中将标题去掉
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item> //去掉ActionBar
<item name="windowNoTitle">true</item> //去掉标题
</style>
扩展一:将Toolbar换成ImageView和TabLayout实现图片显示隐藏
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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:src="@mipmap/bg"
android:scaleType="fitXY"
app:layout_scrollFlags="scroll|exitUntilCollapsed" //将layout_scrollFlags属性加在ImageView上
android:layout_height="200dp" />
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:background="#fff"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="分组一"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="分组二"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="分组三"
android:layout_height="wrap_content" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycle"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
网友评论