Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
各位早上好,話不多說,先上效果圖:

注意頂部:首頁(yè)TextView的變化(顯示和隱藏)!
首先分析下:UI狀態(tài),其是由RecyclerView添加頭部組成+RecyclerView
頭部添加和RecyclerView分別引用如下:具體的分裝數(shù)據(jù)的過程這里就不在說明,下篇博客會(huì)更加深入的寫關(guān)于
RecyclerView總添加多種不同type類型
compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.1' compile 'com.android.support:recyclerview-v7:25.3.1'
第一步:生成布局視圖
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#f2f2f2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:background="#ffffff"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center_horizontal"
tools:visibility="visible"
android:textColor="#000000"
android:textSize="20sp"
android:id="@+id/home_text"
android:visibility="invisible"
android:text="首頁(yè)"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
第二步:直接上其他主要的代碼,其注釋很詳細(xì),不多說
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private int mTextViewHeight;
private RecyclerView mRecyclerView;
private RecyclerViewHeader mRecyclerViewHeader;
//頭部圖片(輪播圖的高度)
private int mRecyclerHeaderBannerHeight;
//頭部的高度
private int mRecyclerHeaderHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.home_text);
//recyclerView填充數(shù)據(jù)(忽略不計(jì))
initData();
mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
//獲取到文本的高度
mTextViewHeight = mTextView.getHeight();
//輪播圖片的高度--和xml圖片的高度是一樣的
mRecyclerHeaderBannerHeight = (int) getResources().getDimension(R.dimen.home_page_banner_height);
//RecyclerView每個(gè)Item之間的距離,和Adapter中設(shè)置的距離一樣
final int recyclerItemHeight = (int) getResources().getDimension(R.dimen.home_page_list_item_margin_top);
//添加頭部視圖,其布局文件就忽略
mRecyclerViewHeader = RecyclerViewHeader.fromXml(this, R.layout.list_item_prime_product_header);
//設(shè)置其滑動(dòng)事件
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//設(shè)置其透明度
float alpha = 0;
int scollYHeight = getScollYHeight(true, mRecyclerHeaderHeight);
//起始截止變化高度,如可以變化高度為mRecyclerHeaderHeight
int baseHeight = mRecyclerHeaderBannerHeight - recyclerItemHeight - mTextViewHeight;
if(scollYHeight >= baseHeight) {
//完全不透明
alpha = 1;
}else {
//產(chǎn)生漸變效果
alpha = scollYHeight / (baseHeight*1.0f);
}
mTextView.setAlpha(alpha);
super.onScrolled(recyclerView, dx, dy);
}
});
//將頭部視圖添加到RecyclerView中
mRecyclerViewHeader.attachTo(mRecyclerView);
//第一次進(jìn)來其狀態(tài)顯示
mRecyclerViewHeader.post(new Runnable() {
@Override
public void run() {
mRecyclerHeaderHeight = mRecyclerViewHeader.getHeight();
mTextViewHeight = mTextView.getHeight();
mTextView.setVisibility(View.VISIBLE);
mTextView.setAlpha(0);
}
});
}
第三步:獲取其滑動(dòng)的距離
/**
* 計(jì)算RecyclerView滑動(dòng)的距離
* @param hasHead 是否有頭部
* @param headerHeight RecyclerView的頭部高度
* @return 滑動(dòng)的距離
*/
private int getScollYHeight(boolean hasHead, int headerHeight) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
//獲取到第一個(gè)可見的position,其添加的頭部不算其position當(dāng)中
int position = layoutManager.findFirstVisibleItemPosition();
//通過position獲取其管理器中的視圖
View firstVisiableChildView = layoutManager.findViewByPosition(position);
//獲取自身的高度
int itemHeight = firstVisiableChildView.getHeight();
//有頭部
if(hasHead) {
return headerHeight + itemHeight*position - firstVisiableChildView.getTop();
}else {
return itemHeight*position - firstVisiableChildView.getTop();
}
}
以上:就可以實(shí)現(xiàn)其效果!
以上所述是小編給大家介紹的Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android中自定義標(biāo)題欄樣式的兩種方法
- 3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
- Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
- Android中去掉標(biāo)題欄的幾種方法(三種)
- Android 全屏無標(biāo)題欄的三種實(shí)現(xiàn)方法
- Android中隱藏標(biāo)題欄和狀態(tài)欄的方法
- Android 使用CoordinatorLayout實(shí)現(xiàn)滾動(dòng)標(biāo)題欄效果的實(shí)例
- Android ScrollView滑動(dòng)實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變
- Android中隱藏狀態(tài)欄和標(biāo)題欄的方法匯總(隱藏狀態(tài)欄、標(biāo)題欄的五種方法)
- Android實(shí)現(xiàn)可折疊式標(biāo)題欄
相關(guān)文章
android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
本篇文章主要介紹了android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Android studio案例之實(shí)現(xiàn)電話撥號(hào)
這篇文章主要介紹了Android studio案例之實(shí)現(xiàn)電話撥號(hào),并有詳細(xì)的步驟和實(shí)現(xiàn)代碼,對(duì)此感興趣的同學(xué),可以參考下2021-04-04
Android自定義滾動(dòng)選擇器實(shí)例代碼
本篇文章主要介紹了Android自定義滾動(dòng)選擇器實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android開發(fā)中模仿qq列表信息滑動(dòng)刪除功能
這篇文章主要介紹了Android開發(fā)中模仿qq列表信息滑動(dòng)刪除功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
android實(shí)現(xiàn)用戶體驗(yàn)超棒的微信WebView進(jìn)度條
本篇文章主要介紹了android實(shí)現(xiàn)用戶體驗(yàn)超棒的微信WebView進(jìn)度條,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android實(shí)現(xiàn)九宮格橫向左右滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)九宮格橫向左右滑動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn)
本篇文章主要介紹了詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8)
這篇文章主要介紹了Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android9.0上針對(duì)Toast的特殊處理圖文詳解
這篇文章主要給大家介紹了關(guān)于Android9.0上針對(duì)Toast的特殊處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android仿微博首頁(yè)Tab加號(hào)彈窗功能
這篇文章主要為大家詳細(xì)介紹了Android仿微博首頁(yè)Tab加號(hào)彈窗功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

