Android標(biāo)題欄隨著滑動(dòng)顯示隱藏

這次實(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)系作者
  • 序言:七十年代末泡徙,一起剝皮案震驚了整個(gè)濱河市径筏,隨后出現(xiàn)的幾起案子该肴,更是在濱河造成了極大的恐慌情竹,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匀哄,死亡現(xiàn)場(chǎng)離奇詭異秦效,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)涎嚼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)阱州,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人铸抑,你說(shuō)我怎么就攤上這事贡耽。” “怎么了鹊汛?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵蒲赂,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我刁憋,道長(zhǎng)滥嘴,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任至耻,我火速辦了婚禮若皱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尘颓。我一直安慰自己走触,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布疤苹。 她就那樣靜靜地躺著互广,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惫皱,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天像樊,我揣著相機(jī)與錄音,去河邊找鬼旅敷。 笑死生棍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的媳谁。 我是一名探鬼主播涂滴,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼韩脑!你這毒婦竟也來(lái)了氢妈?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤段多,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后壮吩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體进苍,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年鸭叙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了觉啊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡沈贝,死狀恐怖杠人,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宋下,我是刑警寧澤嗡善,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站学歧,受9級(jí)特大地震影響罩引,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜枝笨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一袁铐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧横浑,春花似錦剔桨、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至张咳,卻和暖如春帝洪,著一層夾襖步出監(jiān)牢的瞬間似舵,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工葱峡, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留砚哗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓砰奕,卻偏偏與公主長(zhǎng)得像蛛芥,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子军援,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,180評(píng)論 25 707
  • 第十二章主要講了 Material Design 的一些用法 一仅淑、Maternal Design 介紹 ??And...
    radish520like閱讀 1,150評(píng)論 4 8
  • 當(dāng)我們寫(xiě)React應(yīng)用的時(shí)候,知道在組件中何時(shí)使用state何時(shí)不使用state胸哥,是非常重要的涯竟。在這篇文章中,我將...
    mervynyang閱讀 827評(píng)論 0 3
  • 今天,我對(duì)妻子說(shuō) 我們老了嘲更,更應(yīng)該相愛(ài)了 妻說(shuō) 諾 沒(méi)有女人呵護(hù)的男人還是男人嗎 沒(méi)有男人撫摸的女人還是女人嗎 晨...
    木進(jìn)閱讀 273評(píng)論 6 3
  • 在我的意識(shí)里我覺(jué)得我是一個(gè)不太喜歡選擇的這樣一個(gè)人筐钟,但是遠(yuǎn)沒(méi)有到產(chǎn)生“選擇恐懼癥”的地步,不知道是與生俱來(lái)的還...
    奮斗加強(qiáng)版閱讀 144評(píng)論 0 0