Android實(shí)現(xiàn)頂部懸浮效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)頂部懸浮效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout 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:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.activity.NewPersonalCardActivity"> <android.support.design.widget.CoordinatorLayout android:id="@+id/rootLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="192dp" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <LinearLayout android:id="@+id/headLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_collapseMode="pin" app:layout_collapseParallaxMultiplier="0.7"> <include layout="@layout/personal_new_top" /> </LinearLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="44dp" android:background="@color/white" android:navigationIcon="@drawable/icon_back" app:contentInsetLeft="0dp" app:contentInsetStart="0dp" app:layout_collapseMode="pin" app:navigationIcon="@drawable/icon_back"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:singleLine="true" android:textColor="@color/c333333" android:textSize="16sp" tools:text="講師" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> <include layout="@layout/new_personal_tabs" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:scrollbars="none" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> </android.support.v4.widget.SwipeRefreshLayout>
java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ARouter.getInstance().inject(this); setContentView(R.layout.activity_new_personal_card); ButterKnife.bind(this); setSupportActionBar(toolBar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); toolBar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); initData(); } private void initRefresh() { appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (verticalOffset >= 0) { swipeRefreshLayout.setEnabled(true); } else { swipeRefreshLayout.setEnabled(false); } } }); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { initData(); swipeRefreshLayout.setRefreshing(false); } }); }
注意事項(xiàng)
1.清單文件需要配置
android:theme="@style/personalCard"
<style name="personalCard" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
2.gradle配置需要24以上
compileSdkVersion 24 //------------------ 在線依賴開始 ---------------- compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.android.support:recyclerview-v7:24.2.0' compile 'com.android.support:cardview-v7:24.2.0' compile 'com.android.support:design:24.2.0'
3.recyclerView的下滑和swipRefreshLayout的下拉刷新會(huì)沖突,所以需要監(jiān)聽appBarLayout的位移,當(dāng)位移等于0時(shí),即appBarLayout全部展示,可以下拉刷新
4.string.xml
切記
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿eleme點(diǎn)餐頁面二級(jí)聯(lián)動(dòng)列表
本站一直在點(diǎn)外賣,于是心血來潮就像仿餓了么做個(gè)站,接下來通過本文給大家介紹android 二級(jí)聯(lián)動(dòng)列表,仿eleme點(diǎn)餐頁面的相關(guān)資料,需要的朋友可以參考下2016-10-10Flutter上的數(shù)據(jù)監(jiān)控深入理解
這篇文章主要給大家介紹了關(guān)于Flutter上的數(shù)據(jù)監(jiān)控的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Android使用Spinner實(shí)現(xiàn)城市級(jí)聯(lián)下拉框
這篇文章主要為大家詳細(xì)介紹了Android使用Spinner實(shí)現(xiàn)城市級(jí)聯(lián)下拉框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
這篇文章介紹了Android中自定義對(duì)話框(Dialog)的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法
下面小編就為大家分享一篇ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊功能(在之前的微信頁面基礎(chǔ)之上)
這篇文章主要介紹了Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊(在之前的微信頁面基礎(chǔ)之上),本文通過截圖實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2020-03-03Android 高版本API方法在低版本系統(tǒng)上的兼容性處理
本文主要介紹Android 高版本API方法在低版本系統(tǒng)上的兼容性處理的問題,這里提供了解決辦法,并附簡單示例,來詳細(xì)說明解決問題步驟,有需要的小伙伴可以參考下2016-09-09android圖像繪制(六)獲取本地圖片或拍照圖片等圖片資源
從SD卡中獲取圖片資源,或者拍一張新的圖片,然后再進(jìn)行處理(直接處理返回圖片/獲得圖片的地址再處理)接下來為您詳細(xì)介紹,感興趣的朋友可以了解下2013-01-01