編輯:關於 Android 程式設計
很多電商類 App 的首頁都有這種細節:頂部搜尋欄一開始藏起來,隨著列表往上滑、頭部區塊逐漸離開視線,搜尋欄才慢慢浮現並變得不透明。下面用一個 Fragment 把這個效果做出來。
一、Fragment 的 XML 版面
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:ptr="https://schemas.android.com/apk/res-auto" tools:context=".fragment.home.HomeStoreFragment" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv_home_store_list" android:layout_width="match_parent" android:layout_height="match_parent" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimation /> <!--top 搜索欄--> <LinearLayout android:id="@+id/ll_top_search" android:layout_width="match_parent" android:layout_height="60dp" android:background="@color/zuti" android:visibility="invisible" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:background="@drawable/shape_edit_cornor" android:gravity="center" > <ImageView android:id="@+id/iv_search_icon" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon_navbar_search" android:layout_marginRight="5dp" /> <EditText android:id="@+id/et_store_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="輸入商家或商品名" android:textColorHint="@color/shenhui" android:background="@null" /> </LinearLayout> </LinearLayout> </RelativeLayout>
二、核心邏輯
關鍵在於即時算出 headView 底邊距離父容器頂端的位置,再用這個數值去除搜尋欄高度,換算出 alpha 透明度。
private boolean isFlingScroll; private View headView; private PullToRefreshListView lvHomeStore; initView(){ lvHomeStore = (PullToRefreshListView) view.findViewById(R.id.lv_home_store_list); lvHomeStore.setMode(PullToRefreshBase.Mode.BOTH); ListView listView = lvHomeStore.getRefreshableView(); headView = initHeadView(); AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);//這句要加上去 headView.setLayoutParams(layoutParams); listView.addHeaderView(headView); lvHomeStore.setAdapter(adapter); lvHomeStore.setOnScrollListener(this); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_FLING) {//手指離開手機界面,Listview還在滑動 isFlingScroll = true; } else if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {//手指在界面上滾動的情況 isFlingScroll = false; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { showSearchBarShow(); } private void showSearchBarShow() { int headBottomToParentTop = headView.getHeight() + headView.getTop(); Log.d("homeStore", "headView.getHeight(): " + headView.getHeight()); Log.d("homeStore", "headView.getTop(): " + headView.getTop()); Log.d("homeStore", "headBottomToParentTop: " + headBottomToParentTop); if (!isFlingScroll) {//手指在界面滑動的情況 int height = layoutSearch.getHeight(); Log.d("homeStore", "height: " + height); if (headBottomToParentTop > height) { layoutSearch.setVisibility(View.INVISIBLE); } else if (headBottomToParentTop <= height) {//緩慢滑動,這部分代碼工作正常,快速滑動,裡面的數據就跟不上節奏了。 float alpha = (height - headBottomToParentTop) * 1f / height; Log.d("homeStore", "alpha: " + alpha); layoutSearch.setAlpha(alpha); layoutSearch.setVisibility(View.VISIBLE); } if (!headView.isShown()){//解決快速滑動,上部分代碼不能正常工作的問題。 layoutSearch.setAlpha(1); layoutSearch.setVisibility(View.VISIBLE); } } else {//手指離開,listview還在滑動,一般情況是列表快速滑動,這種情況直接設置導航欄的可見性 if (!headView.isShown()) { if (!layoutSearch.isShown()){ layoutSearch.setVisibility(View.VISIBLE); layoutSearch.setAlpha(1); } } else { if (layoutSearch.isShown()){ layoutSearch.setVisibility(View.INVISIBLE); } } } }
要留意的是,慢速滑動時透明度計算很順,但快速滑動(SCROLL_STATE_FLING)時資料回來得比手勢慢一拍,所以額外判斷 headView 是否可見來兜底:一旦頭部已經完全離開畫面,就把搜尋欄直接設為全不透明。
以上就是這次的分享,若有疑問歡迎留言討論。
RecyclerView 入門初探:與 ListView 的差異及實作要點
在做 Material Design 規範開發時,例如先前提到的 CoordinatorLayout 向上折疊效果,若繼續沿用 ListView 是無法實現的,因為它並不
Android 自訂圓形進度條實作:從繪製到動畫
在實際專案中,為了滿足客戶需求與提升使用體驗,幾乎一定會需要自訂 View。本文以圓形進度條為例,拆解自訂 View 的完整繪製流程。 整體思路:三個部分 一個圓形進度
Android startActivityForResult 完整指南:頁面跳轉回傳資料與頭像上傳實作
Android 開發中有一類很典型的互動:跳到另一個畫面填點東西,完成後帶著資料回到原本的畫面。最常見的例子就是登入流程——在「我的」頁面點了需要權限的功能,跳去登入頁,
Android Handler 入門:第一個訊息傳遞範例
這是一個最簡單的 Handler 範例,目的只是讓初學者對 Handler 有個初步的認識。範例在布局上放了一個按鈕,點擊後透過 Handler 傳送資料並列印出來(全部