Android開(kāi)發(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) {//手指離開(kāi)手機(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),上部分代碼不能正常工作的問(wèn)題。 layoutSearch.setAlpha(1); layoutSearch.setVisibility(View.VISIBLE); } } else {//手指離開(kāi),listview還在滑動(dòng),一般情況是列表快速滑動(dòng),這種情況直接設(shè)置導(dǎo)航欄的可見(jiàn)性 if (!headView.isShown()) { if (!layoutSearch.isShown()){ layoutSearch.setVisibility(View.VISIBLE); layoutSearch.setAlpha(1); } } else { if (layoutSearch.isShown()){ layoutSearch.setVisibility(View.INVISIBLE); } } } }
以上所述是小編給大家介紹的Android開(kāi)發(fā)之ListView的head消失頁(yè)面導(dǎo)航欄的漸變出現(xiàn)和隱藏,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(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)畫(huà)
- Android Textview實(shí)現(xiàn)顏色漸變滾動(dòng)效果
- Android中recyclerView底部添加透明漸變效果
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android自定義view漸變圓形動(dòng)畫(huà)
相關(guān)文章
詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類(lèi)的封裝
這篇文章主要介紹了詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類(lèi)的封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android編程動(dòng)態(tài)加載布局實(shí)例詳解【附demo源碼】
這篇文章主要介紹了Android編程動(dòng)態(tài)加載布局,結(jié)合實(shí)例形式分析了Android動(dòng)態(tài)加載布局的原理、操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10android USB如何修改VID具體實(shí)現(xiàn)
在android 設(shè)備的Linux 內(nèi)核中把 USB 驅(qū)動(dòng)的 PID VID 修改以后,也許之前的adb工具就不能識(shí)別設(shè)備了,會(huì)打印出"device not found"的提示2013-06-06Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫(huà)的實(shí)現(xiàn)
這篇文章主要介紹了Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫(huà)的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-10-10Android 關(guān)于ExpandableListView去掉里頭分割線(xiàn)的方法
下面小編就為大家?guī)?lái)一篇Android 關(guān)于ExpandableListView去掉里頭分割線(xiàn)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Android自定義View 仿QQ側(cè)滑菜單的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義View 仿QQ側(cè)滑菜單的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-08-08Android 模擬器(emulator-5554...)出現(xiàn)錯(cuò)誤解決辦法
這篇文章主要介紹了Android 模擬器出現(xiàn)錯(cuò)誤解決辦法的相關(guān)資料,如:Unable to get view server version from device,F(xiàn)ailed to install helloworld.apk on device 'emulator-5554': timeout,這種常見(jiàn)錯(cuò)誤,解決辦法,需要的朋友可以參考下2016-11-11