Android開發(fā)之ListView的head消失頁(yè)面導(dǎo)航欄的漸變出現(xiàn)和隱藏
1.Fragment頁(yè)面xml布局:
<RelativeLayout 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" xmlns:ptr="http://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:ptrAnimationStyle="flip" /> <!--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>
2.主要代碼:
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) {//手指離開手機(jī)界面,Listview還在滑動(dòng)
isFlingScroll = true;
} else if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {//手指在界面上滾動(dòng)的情況
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) {//手指在界面滑動(dòng)的情況
int height = layoutSearch.getHeight();
Log.d("homeStore", "height: " + height);
if (headBottomToParentTop > height) {
layoutSearch.setVisibility(View.INVISIBLE);
} else if (headBottomToParentTop <= height) {//緩慢滑動(dòng),這部分代碼工作正常,快速滑動(dòng),里面的數(shù)據(jù)就跟不上節(jié)奏了。
float alpha = (height - headBottomToParentTop) * 1f / height;
Log.d("homeStore", "alpha: " + alpha);
layoutSearch.setAlpha(alpha);
layoutSearch.setVisibility(View.VISIBLE);
}
if (!headView.isShown()){//解決快速滑動(dòng),上部分代碼不能正常工作的問題。
layoutSearch.setAlpha(1);
layoutSearch.setVisibility(View.VISIBLE);
}
} else {//手指離開,listview還在滑動(dòng),一般情況是列表快速滑動(dòng),這種情況直接設(shè)置導(dǎo)航欄的可見性
if (!headView.isShown()) {
if (!layoutSearch.isShown()){
layoutSearch.setVisibility(View.VISIBLE);
layoutSearch.setAlpha(1);
}
} else {
if (layoutSearch.isShown()){
layoutSearch.setVisibility(View.INVISIBLE);
}
}
}
}
以上所述是小編給大家介紹的Android開發(fā)之ListView的head消失頁(yè)面導(dǎo)航欄的漸變出現(xiàn)和隱藏,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android ScrollView滑動(dòng)實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變
- Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
- Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果
- Android UI設(shè)計(jì)系列之自定義ListView仿QQ空間阻尼下拉刷新和漸變菜單欄效果(8)
- Android自定義漸變式炫酷ListView下拉刷新動(dòng)畫
- Android Textview實(shí)現(xiàn)顏色漸變滾動(dòng)效果
- Android中recyclerView底部添加透明漸變效果
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android自定義view漸變圓形動(dòng)畫
相關(guān)文章
Android實(shí)現(xiàn)360手機(jī)助手底部的動(dòng)畫菜單
這篇文章給大家分享了利用Android實(shí)現(xiàn)360手機(jī)助手底部的動(dòng)畫菜單效果,文中給出了實(shí)例代碼,這樣對(duì)大家的學(xué)習(xí)或者理解更有幫助,有需要的朋友們下面來(lái)一起看看吧。2016-10-10
Kotlin?Dispatchers協(xié)程調(diào)度器源碼深入分析
Kotlin協(xié)程不是什么空中閣樓,Kotlin源代碼會(huì)被編譯成class字節(jié)碼文件,最終會(huì)運(yùn)行到虛擬機(jī)中。所以從本質(zhì)上講,Kotlin和Java是類似的,都是可以編譯產(chǎn)生class的語(yǔ)言,但最終還是會(huì)受到虛擬機(jī)的限制,它們的代碼最終會(huì)在虛擬機(jī)上的某個(gè)線程上被執(zhí)行2022-11-11
Android 中解決Viewpage調(diào)用notifyDataSetChanged()時(shí)界面無(wú)刷新的問題
這篇文章主要介紹了Android 中解決Viewpage調(diào)用notifyDataSetChanged()時(shí)界面無(wú)刷新的問題的相關(guān)資料,這里提供相應(yīng)的解決辦法,需要的朋友可以參考下2017-08-08
Android開發(fā)應(yīng)用中Broadcast Receiver組件詳解
本篇文章主要介紹了Android開發(fā)應(yīng)用中Broadcast Receiver組件詳解,想要學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11
分析Android 11.0Settings源碼之主界面加載
這篇文章主要介紹了分析Android 11.0Settings源碼之主界面加載,對(duì)Android源碼感興趣的同學(xué),可以著重看一下2021-04-04
Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法
這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-05-05
Android nativePollOnce函數(shù)解析
這篇文章主要介紹了Android nativePollOnce函數(shù)解析的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03

