這次實(shí)現(xiàn)標(biāo)題欄隨著上滑下滑顯示隱藏
實(shí)現(xiàn)這個(gè)效果需要株扛,Support Design庫(kù)中的CoordinatorLayout和AppBarLayout進(jìn)行配合才行摇展。
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="我是標(biāo)題"
app:titleTextColor="#fff"
android:id="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways" //這個(gè)屬性實(shí)現(xiàn)隨著頁(yè)面滾動(dòng)標(biāo)題欄(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"http://這個(gè)屬性只有在布局是CoordinatorLayout 時(shí)才有畸肆,是讓讓控件在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);
}
}
還需要在主題中將標(biāo)題去掉
<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> //去掉標(biāo)題
</style>
擴(kuò)展一:將Toolbar換成ImageView和TabLayout實(shí)現(xiàn)圖片顯示隱藏
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>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者