栗子——ListView+Head滑動(dòng)顯示標(biāo)題

項(xiàng)目里的效果拎出來做了個(gè)栗子,拿來和大家一起分享,一個(gè)很簡(jiǎn)單的栗子,不喜勿噴~

栗子配圖.png

栗子慣例,先上GIF

栗子.gif

代碼分析

其實(shí)核心的地方也是獲取ListView的垂直滾動(dòng)距離,在獲取到滾動(dòng)距離以后,根據(jù)垂直滾動(dòng)距離來設(shè)置標(biāo)題欄的背景透明度。

參考代碼:感謝作者~ListView 獲取精確的垂直滾動(dòng)距離,但是有個(gè)BUG,待會(huì)說,代碼中也會(huì)標(biāo)明,不用擔(dān)心錯(cuò)過BUG~


activity_main.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/lvTitleFade"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/rlTitle"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimary" >

        <ImageButton
            android:src="@drawable/back"
            android:background="@null"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:gravity="center"
            android:text="FJTitleFade"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>
</FrameLayout>

說明:主布局很簡(jiǎn)單,布局選用FrameLayout,讓標(biāo)題欄在ListView的上方~


MainActivity.java核心代碼

private SparseArray recordSp = new SparseArray(0);
private int mCurrentfirstVisibleItem = 0;

//設(shè)置標(biāo)題背景透明
rlTitle.getBackground().setAlpha(0);
//滑動(dòng)監(jiān)聽,注意implements OnScrollListener
lvTitleFade.setOnScrollListener(this);

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    
}
//滑動(dòng)事件處理
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    //firstVisibleItem--處于頂部的Item標(biāo)記
    //visibleItemCount--當(dāng)前可見item數(shù)
    //totalItemCount----總item數(shù)
    mCurrentfirstVisibleItem = firstVisibleItem;
    View firstView = view.getChildAt(0);
    if (null != firstView) {
        ItemRecod itemRecord = (ItemRecod) recordSp.get(firstVisibleItem);
        if (null == itemRecord) {
            itemRecord = new ItemRecod();
        }
        itemRecord.height = firstView.getHeight();//獲取最頂部Item的高度
        itemRecord.top = firstView.getTop();//獲取距離頂部的距離
        recordSp.append(firstVisibleItem, itemRecord);//設(shè)置值
    }
    Log.d("dmdrs", "滑動(dòng)距離:" + getScrollY());
    int ScrollY = getScrollY();
    if (ScrollY >= 0 && ScrollY <= 255) {
        //設(shè)置標(biāo)題欄透明度0~255
        rlTitle.getBackground().setAlpha(ScrollY);
    } else if (ScrollY > 255) {
        //滑動(dòng)距離大于255就設(shè)置為不透明
        rlTitle.getBackground().setAlpha(255);
    }
}

private int getScrollY() {
    int height = 0;
    for (int i = 0; i < mCurrentfirstVisibleItem; i++) {
        ItemRecod itemRecod = (ItemRecod) recordSp.get(i);
        Log.d("dmdrs", "xxx1:" + itemRecod);
        //06-07 21:00:21.601: D/dmdrs(23096): xxx1:
        //          com.dmdrs.titlefade.MainActivity$ItemRecod@529122fc
        //06-07 21:00:21.601: D/dmdrs(23096): xxx2:300
        //06-07 21:00:21.601: D/dmdrs(23096): xxx1:null
        //快速滑動(dòng)會(huì)為空,判斷一下,發(fā)現(xiàn)的bug
        if(itemRecod != null){
            height += itemRecod.height;
        }
        Log.d("dmdrs", "xxx2:" + height);
    }
    ItemRecod itemRecod = (ItemRecod) recordSp.get(mCurrentfirstVisibleItem);
    if (null == itemRecod) {
        itemRecod = new ItemRecod();
    }
    return height - itemRecod.top;
}

class ItemRecod {
    int height = 0;
    int top = 0;
}

說明:
①設(shè)置標(biāo)題背景透明為完全透明
②設(shè)置監(jiān)聽ListView的滑動(dòng)事件
③在onScroll里來獲取當(dāng)前Item的參數(shù),設(shè)置到SparseArray中,然后調(diào)用getScrollY方法來計(jì)算滑動(dòng)距離并返回參數(shù),然后根據(jù)距離來設(shè)置rlTitle的透明度。


總結(jié):就上面這點(diǎn)了,代碼不很多,但實(shí)現(xiàn)了想要的效果。在擼代碼的過程中也發(fā)現(xiàn)了個(gè)小BUG,在快速滑動(dòng)ListView的時(shí)候itemRecod會(huì)出現(xiàn)為null的情況。這個(gè)BUG出現(xiàn)是因?yàn)榛瑒?dòng)過快,導(dǎo)致值還沒有設(shè)置進(jìn)去,就在取值,所以出現(xiàn)了null的情況,導(dǎo)致空指針錯(cuò)誤。目前解決辦法是加了個(gè)if判斷null解決了這個(gè)小BUG,如果有好的解決方式請(qǐng)留言交流,謝謝~~


未經(jīng)本人允許禁止轉(zhuǎn)載,違者必究


簡(jiǎn)書:www.itdecent.cn/u/3d2770e6e489

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容