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 //------------------ 在線依賴開(kāi)始 ---------------- 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)聽(tīng)appBarLayout的位移,當(dāng)位移等于0時(shí),即appBarLayout全部展示,可以下拉刷新
4.string.xml
切記
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Studio實(shí)現(xiàn)登錄界面功能
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)登錄界面功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android 中Lambda表達(dá)式的使用實(shí)例詳解
這篇文章主要介紹了 Android 中Lambda表達(dá)式的使用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android6.0獲取動(dòng)態(tài)權(quán)限代碼示例
這篇文章主要介紹了Android6.0以上獲取動(dòng)態(tài)權(quán)限代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android Handler內(nèi)存泄漏詳解及其解決方案
在android開(kāi)發(fā)過(guò)程中,我們可能會(huì)遇到過(guò)令人奔潰的OOM異常,這篇文章主要介紹了Android Handler內(nèi)存泄漏詳解及其解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android應(yīng)用接入微信分享的實(shí)例代碼
本篇文章主要介紹了Android應(yīng)用接入微信分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-07-07
Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例
最近看到豆瓣FM的音樂(lè)播放界面,有一個(gè)環(huán)形的進(jìn)度條挺不錯(cuò)的,最近有空就想著實(shí)現(xiàn)了,所以下面這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
Android ProgressBar 模擬進(jìn)度條效果的實(shí)現(xiàn)
這篇文章主要介紹了Android ProgressBar 模擬進(jìn)度條效果的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Android Studio 報(bào)錯(cuò)failed to create jvm error code -4的解決方法
這篇文章主要介紹了Android Studio 在安裝后啟動(dòng)報(bào)錯(cuò)failed to create jvm error code -4的解決方法,需要的朋友可以參考下2014-05-05

